Rust: Getting the factorial of n

Yes, I know writing a factorial function is pretty trivial, but since I am learning Rust from scratch, it is the second program I wrote on it (after the “Hello world”).

This is the code for a recursive factorial in Rust:

fn main()
{
    let r = factorial(9);
    println!("{}", r);
}

fn factorial(n : i64) -> i64
{
    if n == 1 {
        1
    }
    else {
        n * factorial(n - 1)
    }
}

Though pretty simple, this code contains several interesting things, so, let’s explore them.

Notice the line:

let r = factorial(9);

In such line I am creating a new variable called “r”. The let keyword indicates we are declaring a new variable. As you can se, I am not specifying the type of my variable and I do not need to do it because the compiler infers the type of “r” based on what the function returns (in this case, a 64-bit integer). Since Rust is a statically typed language, the inferred type of “r” will not change during its lifetime (similar to how “auto” in C++ works).

Notice that I invoke the function “factorial” that is declared below my call. Contrarily to how C and C++ work, you do not need to define or declare the functions before being invoked.

See this line:

fn factorial(n : i64) -> i64

In that line I am declaring my function factorial. The n : i64 says that there is an argument called “n” that is a 64-bit integer. The -> i64 piece of code says this function returns another 64-bit integer. If you do not specify anything, the function returns nothing (it would be a “void” function in the C++ world).

One weird thing IMHO, is that the “if” block does not require parenthesis in the expression to be evaluated.

Notice also that inside the “if” block and inside the “else” block, the expressions are simply declared right there and do not have an ending semicolon. That is because in Rust, if you do not write down a semicolon, you are writing an expression that will be returned, otherwise, you are writing a statement. So, for example, this C++ construction:

auto a = b == 2 ? 3 : 5;

In Rust could be expressed as:

let a = if b == 2 { 3 } else { 5 };

So, in my function factorial, if n is equal to 1, all the function will return 1 otherwise will return n times the result of factorial of n – 1.

In a more-C++ish way, you could declare the factorial function similar to this:

fn factorial(n : i64) -> i64
{
    if n == 1 {
        return 1;
    }
   
    return n * factorial(n - 1);
}

In this case, notice the “return” statements and the semicolons after them.

In Rust, if no “return” is explicitly written, the last expression in the function is returned.

So, in the code below, the functions p and q are equivalent:

fn p() -> i32
{
  return 126;
}

fn q() -> i32
{
  126
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s