Skip to content

Commit bbce232

Browse files
authored
types-and-values: add explanatory commentary to solution (#3067)
This commentary, written by Gemini, focuses on aspects of the solution that differ from the baseline languages (C/Java/Python), highlighting Rust-specific idioms and concepts.
1 parent db49131 commit bbce232

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/types-and-values/solution.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
{{#include exercise.rs:solution}}
55
```
66

7+
We use the `return` syntax here to return values from the function. Later in the
8+
course, we will see that the last expression in a block is automatically
9+
returned, allowing us to omit the `return` keyword for a more concise style.
10+
11+
The `if` condition `n < 2` does not need parentheses, which is standard Rust
12+
style.
13+
14+
## Panic
15+
16+
The exercise asks when this function will panic. The Fibonacci sequence grows
17+
very rapidly. With `u32`, the calculated values will overflow the 32-bit integer
18+
limit (4,294,967,295) when `n` reaches 48.
19+
20+
In Rust, integer arithmetic checks for overflow in _debug mode_ (which is the
21+
default when using `cargo run`). If an overflow occurs, the program will panic
22+
(crash with an error message). In _release mode_ (`cargo run --release`),
23+
overflow checks are disabled by default, and the number will wrap around
24+
(modular arithmetic), producing incorrect results.
25+
726
<details>
827

928
- Walk through the solution step-by-step.

0 commit comments

Comments
 (0)