Skip to content

Commit 19116d2

Browse files
authored
Add speaker notes to Fibonacci exercise (#2898)
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.
1 parent 6ab9858 commit 19116d2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/types-and-values/exercise.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ this function panic?
2323
2424
{{#include exercise.rs:main}}
2525
```
26+
27+
<details>
28+
29+
- This exercise is a classic introduction to recursion.
30+
- Encourage students to think about the base cases and the recursive step.
31+
- The question "When will this function panic?" is a hint to think about integer
32+
overflow. The Fibonacci sequence grows quickly!
33+
- Students might come up with an iterative solution as well, which is a great
34+
opportunity to discuss the trade-offs between recursion and iteration (e.g.,
35+
performance, stack overflow for deep recursion).
36+
37+
</details>

src/types-and-values/solution.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,21 @@
33
```rust,editable
44
{{#include exercise.rs:solution}}
55
```
6+
7+
<details>
8+
9+
- Walk through the solution step-by-step.
10+
- Explain the recursive calls and how they lead to the final result.
11+
- Discuss the integer overflow issue. With `u32`, the function will panic for
12+
`n` around 47. You can demonstrate this by changing the input to `main`.
13+
- Show an iterative solution as an alternative and compare its performance and
14+
memory usage with the recursive one. An iterative solution will be much more
15+
efficient.
16+
17+
## More to Explore
18+
19+
For a more advanced discussion, you can introduce memoization or dynamic
20+
programming to optimize the recursive Fibonacci calculation, although this is
21+
beyond the scope of the current topic.
22+
23+
</details>

0 commit comments

Comments
 (0)