我是 Rust 的新手,但作为 Haskell 的粉丝,我非常欣赏match
Rust 的工作方式。现在我面临着我确实需要失败的罕见情况——从某种意义上说,我希望执行几个重叠的所有匹配情况。这有效:
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
}
我的问题是这是否在 Rust 中是惯用的,或者是否有更好的方法。
上下文是Cracking the Coding Interview中的一个问题:“一个孩子正在跑上n步的楼梯,一次可以跳 1 步、2 步或 3 步。实现一种方法来计算孩子可以用多少种可能的方式跑上楼梯。”</p>