typestar

Iterative Fibonacci in Rust

Fibonacci without recursion, and a first look at shadowing.

fn fib(n: u32) -> u64 {
    let (mut a, mut b) = (0u64, 1u64);
    for _ in 0..n {
        let next = a + b;
        a = b;
        b = next;
    }
    a
}

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
115
Tokens
52
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 35 seconds.

Type this snippet

Step 3 of 4 in Functions, step 25 of 39 in Language basics.

← Previous Next →

Iterative Fibonacci in other languages