rustfibonacciidiomsfall-through

Idiomatic implementation of the tribonacci sequence in Rust


I’m new to Rust, but as a fan of Haskell, I greatly appreciate the way match works in Rust. Now I’m faced with the rare case where I do need fall-through – in the sense that I would like all matching cases of several overlapping ones to be executed. This works:

fn options(stairs: i32) -> i32 {
    if stairs == 0 {
        return 1;
    }
    let mut count: i32 = 0;
    if stairs >= 1 {
        count += options(stairs - 1);
    }
    if stairs >= 2 {
        count += options(stairs - 2);
    }
    if stairs >= 3 {
        count += options(stairs - 3);
    }
    count
}

My question is whether this is idiomatic in Rust or whether there is a better way.

The context is a question from Cracking the Coding Interview: “A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.”


Solution

  • Based on the definition of the tribonacci sequence I found you could write it in a more concise manner like this:

    fn options(stairs: i32) -> i32 {
        match stairs {
            0 => 0,
            1 => 1,
            2 => 1,
            3 => 2,
            _ => options(stairs - 1) + options(stairs - 2) + options(stairs - 3)
        }
    }
    

    I would also recommend changing the funtion definition to only accept positive integers, e.g. u32.