February 2022
Intermediate to advanced
140 pages
2h 28m
English
| Puzzle 23 | Constant Loops |
| | const fn fib(n: u128) -> u128 { |
| | let mut a = 1; |
| | let mut b = 1; |
| | for _ in 2..n { |
| | let tmp = a + b; |
| | a = b; |
| | b = tmp; |
| | } |
| | b |
| | } |
| | |
| | fn main() { |
| | for i in 0..5 { |
| | println!("Fib {} = {}", i, fib(i)); |
| | } |
| | } |
Guess the Output | |
|---|---|
|
|
Try to guess what the output is before moving to the next page. |
The program will fail to compile with the following error message:
| | 'for' is not allowed in a 'const fn' |
Marking a function as const causes the function to run at compile ...
Read now
Unlock full access