this post was submitted on 25 Apr 2025
1 points (100.0% liked)

The Rust Programming Language

40 readers
1 users here now

A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and...

founded 2 years ago
MODERATORS
 
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
 }
 }
}
no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here