This is an automated archive made by the Lemmit Bot.
The original was posted on /r/rust by /u/LordMoMA007 on 2025-04-25 01:27:40+00:00.
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
}
}
}
You must log in or register to comment.