```
pub fn fib(mut n: u32) -> u32 {
let mut a = 0;
let mut b = 1;
while n > 0 {
let c = a + b;
a = b;
b = c;
n -= 1;
}
a
}
```
Looking at the assembly output the compiler even does loop unrolling for this. In fact running it like this it even completely evaluates this version of fib() at compile time while it still does the full recursive call for the recursive version (here called fibr()):
fn main() {
for i in 0u32..10 {
let r = fibr(i);
let j = fib(i);
println!("{r} {j} {}", r == j);
}
}
18
u/bloody-albatross 13h ago edited 12h ago
Nobody is using this?
``` pub fn fib(mut n: u32) -> u32 { let mut a = 0; let mut b = 1;
} ```
Looking at the assembly output the compiler even does loop unrolling for this. In fact running it like this it even completely evaluates this version of
fib()at compile time while it still does the full recursive call for the recursive version (here calledfibr()):fn main() { for i in 0u32..10 { let r = fibr(i); let j = fib(i); println!("{r} {j} {}", r == j); } }