1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-09-16 09:36:41 +02:00

Add speaker notes to Fibonacci exercise

The speaker notes should prompt a deeper discussion around the
exercise by calling out the non-obvious integer overflow pitfall and
encourage instructors to explore alternative implementations with
students, moving beyond a surface-level recursive solution.
This commit is contained in:
Martin Geisler
2025-09-09 19:49:54 +02:00
parent 93c5f28b92
commit 193563dadc
2 changed files with 30 additions and 0 deletions

View File

@@ -23,3 +23,15 @@ this function panic?
{{#include exercise.rs:main}}
```
<details>
- This exercise is a classic introduction to recursion.
- Encourage students to think about the base cases and the recursive step.
- The question "When will this function panic?" is a hint to think about integer
overflow. The Fibonacci sequence grows quickly!
- Students might come up with an iterative solution as well, which is a great
opportunity to discuss the trade-offs between recursion and iteration (e.g.,
performance, stack overflow for deep recursion).
</details>

View File

@@ -3,3 +3,21 @@
```rust,editable
{{#include exercise.rs:solution}}
```
<details>
- Walk through the solution step-by-step.
- Explain the recursive calls and how they lead to the final result.
- Discuss the integer overflow issue. With `u32`, the function will panic for
`n` around 47. You can demonstrate this by changing the input to `main`.
- Show an iterative solution as an alternative and compare its performance and
memory usage with the recursive one. An iterative solution will be much more
efficient.
## More to Explore
For a more advanced discussion, you can introduce memoization or dynamic
programming to optimize the recursive Fibonacci calculation, although this is
beyond the scope of the current topic.
</details>