我在项目中需要计算21的阶乘。
fn factorial(num: u64) -> u64 {
match num {
0 => 1,
1 => 1,
_ => factorial(num - 1) * num,
}
}
fn main() {
let x = factorial(21);
println!("The value of 21 factorial is {} ", x);
}
运行此代码时,我遇到了一个错误:
thread 'main' panicked at 'attempt to multiply with overflow', src\main.rs:5:18
u128
而不是u8
? - Daniel X Moore(1..num+1)
:(1..=num)
@FabioPetrillo - Mohsen Bg