get functions
All checks were successful
buildbot/mdbook test Build done.

This commit is contained in:
AF 2023-11-07 06:05:09 +00:00
parent 5524521508
commit b2afa4405d
2 changed files with 45 additions and 0 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

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