src/types-and-values/solution.md
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include exercise.rs:solution}}
We use the return syntax here to return values from the function. Later in the
course, we will see that the last expression in a block is automatically
returned, allowing us to omit the return keyword for a more concise style.
The if condition n < 2 does not need parentheses, which is standard Rust
style.
The exercise asks when this function will panic. The Fibonacci sequence grows
very rapidly. With u32, the calculated values will overflow the 32-bit integer
limit (4,294,967,295) when n reaches 48.
In Rust, integer arithmetic checks for overflow in debug mode (which is the
default when using cargo run). If an overflow occurs, the program will panic
(crash with an error message). In release mode (cargo run --release),
overflow checks are disabled by default, and the number will wrap around
(modular arithmetic), producing incorrect results.
u32, the function will panic for
n around 47. You can demonstrate this by changing the input to main.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>