first code in Rust
Today, I woke up with a thought: learn Rust. I bought a book about 3 months ago and started to read it, browse code on GitHub, etc., but I never wrote any code myself.
After 1 hour (yeah, I suck), I implemented a Fibonacci sequence. I think I used every Rust tip I could remember, but I'm not entirely sure.
This is the bad code I wrote, but I'm very proud of it.
use std::mem;
fn fib(n: &mut i32) -> i32 {
let mut a = 1;
let mut b = 1;
while *n > 1 {
a += b;
mem::swap(&mut a, &mut b);
*n -= 1;
}
b
}
This prints the first \(N\) numbers from the Fibonacci sequence.
println!("{}", fib(&mut N));
Maybe one day, I'll look at this code and think: "WTF is that?!".
Written on April 13, 2020