I'm curious can you really write such compile time code in Rust
I’m curious—can writing an idiomatic fibonacci_compile_time function in Rust actually be that easy? I don't see I could even write code like that in the foreseeable future. How do you improve your Rust skills as a intermediate Rust dev?
// Computing at runtime (like most languages would)
fn fibonacci_runtime(n: u32) -> u64 {
if n <= 1 {
return n as u64;
}
let mut a = 0;
let mut b = 1;
for _ in 2..=n {
let temp = a + b;
a = b;
b = temp;
}
b
}
// Computing at compile time
const fn fibonacci_compile_time(n: u32) -> u64 {
match n {
0 => 0,
1 => 1,
n => {
let mut a = 0;
let mut b = 1;
let mut i = 2;
while i <= n {
let temp = a + b;
a = b;
b = temp;
i += 1;
}
b
}
}
}