Factorial in Rust
Example for versions
Rust 0.1
This example uses recursive factorial definition. Due to uint
overflow values of 13! and larger are calculated incorrectly.
use std;
import std::io;
fn factorial(x: int) -> uint {
if (x <= 1) {
ret 1u;
} else {
ret (x as uint) * factorial(x - 1);
}
}
fn main() {
let i = 0;
while i <= 16 {
io::println(#fmt("%d! = %u", i, factorial(i)));
i = i + 1;
}
}
Comments
]]>blog comments powered by Disqus
]]>