2024-06-18 18:10:13 +08:00

42 lines
873 B
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// iterators4.rs
//
// 執行 `rustlings hint iterators4` 或使用 `hint` 子命令獲取提示。
// I AM NOT DONE
pub fn factorial(num: u64) -> u64 {
// 完成此函數以回傳 num 的階乘
// 不要使用:
// - 提前返回(顯式使用 `return` 關鍵字)
// 嘗試不要使用:
// - 命令式風格的循環for、while
// - 其他變量
// 額外挑戰,不要使用:
// - 遞迴
// 執行 `rustlings hint iterators4` 獲取提示。
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn factorial_of_0() {
assert_eq!(1, factorial(0));
}
#[test]
fn factorial_of_1() {
assert_eq!(1, factorial(1));
}
#[test]
fn factorial_of_2() {
assert_eq!(2, factorial(2));
}
#[test]
fn factorial_of_4() {
assert_eq!(24, factorial(4));
}
}