From 4b99348d886e6a8f9c7b72a1f64b2f2f4960e57b Mon Sep 17 00:00:00 2001 From: parrrate Date: Thu, 9 Apr 2026 12:56:16 +0000 Subject: [PATCH] get functions --- src/SUMMARY.md | 1 + src/exercises/get_functions.md | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/exercises/get_functions.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index e0c1e46..5b2a7c2 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/exercises/get_functions.md b/src/exercises/get_functions.md new file mode 100644 index 0000000..9506494 --- /dev/null +++ b/src/exercises/get_functions.md @@ -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); +} +```