50.4.1First program¶
We first create the binary project using cargo:
cargo init factorial Creating binary (application) package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Then we enter the folder:
cd factorialAnd modify the main code file as follow, with a function fact() called by
the main() function:
bat src/main.rsfn fact(n: u64) -> u64 {
if n > 1 {
n * fact(n - 1)
} else {
1
}
}
fn main() {
println!("{}", fact(20));
}
Finally, we build & execute:
cargo run Compiling factorial v0.1.0 (/tmp/tmp.cBdauJcdkl/factorial)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
Running `target/debug/factorial`
2432902008176640000