Learn how to work with conditional statements using if-else expression.

Example 1: If Else in Rust

This example project will teach you the following concepts:

  1. If-Else in Rust

Step 1: Create Project

The first step is to create a Rust project. We can use Cargo to generate us a project template. Use the following command:

cargo new your_project_name

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

In your src/main.rs type the following code:

main.rs

fn main() {
    // if expressions
    let number = 5;
    if number == 5 {
        println!("number is 5");
    } else if number == 4 {
        println!("number is 4");
    } else {
        println!("number is not 5");
    }

    // let + if
    let on = true;
    let n = if on { 1 } else { 0 };
    println!("n: {}", n);

    // loops
    let mut i = 0;
    loop {
        println!("is universe infinite?");
        i += 1;
        if i == 2 {
            break;
        }
    }

    // loop expressions
    let mut counter = 0;
    let result = loop {
        counter += 1;

        if counter == 2 {
            break counter + 1;
        }
    };
    println!("result: {}", result);

    // while loops
    let mut number = 3;
    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }
    println!("LIFTOFF!!!");

    // for loops
    let a = [1; 3];
    for e in a.iter() {
        println!("e: {}", e);
    }

    // rev()
    for n in (1..4).rev() {
        println!("{}!", n)
    }
    println!("boom!")
}

Run

Save the code and run your code as follows using Cargo:

$ cargo run

Result

You will get the following:

number is 5
n: 1
is universe infinite?
is universe infinite?
result: 3
3!

Categorized in: