Compare commits
3 Commits
fc5f4f7ca3
...
b2afa4405d
Author | SHA1 | Date | |
---|---|---|---|
b2afa4405d | |||
5524521508 | |||
3e6f87cf09 |
@ -11,6 +11,7 @@
|
|||||||
- [BoolStream](./exercises/bool_stream.md)
|
- [BoolStream](./exercises/bool_stream.md)
|
||||||
- [RcChars](./exercises/rcchars.md)
|
- [RcChars](./exercises/rcchars.md)
|
||||||
- [Async Fn](./exercises/async_fn.md)
|
- [Async Fn](./exercises/async_fn.md)
|
||||||
|
- [Get Functions](./exercises/get_functions.md)
|
||||||
- [Chapter 2](./chapter_2.md)
|
- [Chapter 2](./chapter_2.md)
|
||||||
- [AnyStr](./exercises/anystr.md)
|
- [AnyStr](./exercises/anystr.md)
|
||||||
- [Mode](./exercises/mode.md)
|
- [Mode](./exercises/mode.md)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
# `From<&T>`
|
||||||
|
|
||||||
Make this compile and pass tests:
|
Make this compile and pass tests:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
|
44
src/exercises/get_functions.md
Normal file
44
src/exercises/get_functions.md
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
# Get `Functions`
|
||||||
|
|
||||||
|
Edit the body of `get_functions` to make this compile and pass tests:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
struct Functions {
|
||||||
|
five: fn() -> i32,
|
||||||
|
increment: fn(i32) -> i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_functions() -> &'static Functions {
|
||||||
|
# /*
|
||||||
|
let five = || 5;
|
||||||
|
let increment = |n| n + 1;
|
||||||
|
let functions = Functions { five, increment };
|
||||||
|
&functions
|
||||||
|
# */
|
||||||
|
# &Functions { five: || 5, increment: |n| n + 1 }
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!((get_functions().five)(), 5);
|
||||||
|
assert_eq!((get_functions().increment)(4), 5);
|
||||||
|
```
|
||||||
|
|
||||||
|
Try solving it in the playground:
|
||||||
|
|
||||||
|
```rust,editable,compile_fail
|
||||||
|
struct Functions {
|
||||||
|
five: fn() -> i32,
|
||||||
|
increment: fn(i32) -> i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_functions() -> &'static Functions {
|
||||||
|
let five = || 5;
|
||||||
|
let increment = |n| n + 1;
|
||||||
|
let functions = Functions { five, increment };
|
||||||
|
&functions
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert_eq!((get_functions().five)(), 5);
|
||||||
|
assert_eq!((get_functions().increment)(4), 5);
|
||||||
|
}
|
||||||
|
```
|
@ -16,7 +16,7 @@ p.s. most of the exercises are actually about `trait`s, this categorisation isn'
|
|||||||
[`RcChars`]: ./exercises/rcchars.md
|
[`RcChars`]: ./exercises/rcchars.md
|
||||||
[extracting lifetimes]: ./exercises/bool_stream.md
|
[extracting lifetimes]: ./exercises/bool_stream.md
|
||||||
[exclusive traits]: ./exercises/multiple_blanket.md
|
[exclusive traits]: ./exercises/multiple_blanket.md
|
||||||
[merging traits]: ./exercises/modes.md
|
[merging traits]: ./exercises/mode.md
|
||||||
[`AnyStr`]: ./exercises/anystr.md
|
[`AnyStr`]: ./exercises/anystr.md
|
||||||
[composition]: ./exercises/composition.md
|
[composition]: ./exercises/composition.md
|
||||||
[`Duration`]: ./exercises/duration.md
|
[`Duration`]: ./exercises/duration.md
|
||||||
|
Loading…
Reference in New Issue
Block a user