Puzzle 23Constant 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;
 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 ...

Get Rust Brain Teasers now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.