Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

50.4.1First program

We first create the binary project using cargo:

    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:

And modify the main code file as follow, with a function fact() called by the main() function:

fn fact(n: u64) -> u64 {
  if n > 1 {
    n * fact(n - 1)
  } else {
    1
  }
}

fn main() {
  println!("{}", fact(20));
}

Finally, we build & execute:

   Compiling factorial v0.1.0 (/tmp/tmp.cBdauJcdkl/factorial)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.16s
     Running `target/debug/factorial`
2432902008176640000