Compare commits

...

3 Commits

Author SHA1 Message Date
b2afa4405d get functions
All checks were successful
buildbot/mdbook test Build done.
2023-11-07 06:05:09 +00:00
5524521508 fromref header 2023-11-07 05:55:01 +00:00
3e6f87cf09 fix mode topic url 2023-11-07 05:54:52 +00:00
4 changed files with 48 additions and 1 deletions

View File

@ -11,6 +11,7 @@
- [BoolStream](./exercises/bool_stream.md)
- [RcChars](./exercises/rcchars.md)
- [Async Fn](./exercises/async_fn.md)
- [Get Functions](./exercises/get_functions.md)
- [Chapter 2](./chapter_2.md)
- [AnyStr](./exercises/anystr.md)
- [Mode](./exercises/mode.md)

View File

@ -1,3 +1,5 @@
# `From<&T>`
Make this compile and pass tests:
```rust

View 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);
}
```

View File

@ -16,7 +16,7 @@ p.s. most of the exercises are actually about `trait`s, this categorisation isn'
[`RcChars`]: ./exercises/rcchars.md
[extracting lifetimes]: ./exercises/bool_stream.md
[exclusive traits]: ./exercises/multiple_blanket.md
[merging traits]: ./exercises/modes.md
[merging traits]: ./exercises/mode.md
[`AnyStr`]: ./exercises/anystr.md
[composition]: ./exercises/composition.md
[`Duration`]: ./exercises/duration.md