Skip to content

Conditionals & Loops

Conditionals

if / else if / else

let num = 7;

if num < 5 {
    println!("Less than 5");
} else if num == 5 {
    println!("Equal to 5");
} else {
    println!("Greater than 5");
}
  • Conditions must be bool

    (doesn't automatically convert types (int, string, etc), like in some languages)

  • No parentheses needed around condition


match (Powerful pattern matching)

let num = 3;

match num {
    1 => println!("One"),
    2 | 3 => println!("Two or Three"), // Multiple patterns with `|`
    4..=6 => println!("Between 4 and 6"), // Inclusive range
    _ => println!("Anything else"),
}
  • Exhaustive — all cases must be covered (use _ as catch-all)
  • Can match on values, ranges, enums, destructure tuples, structs, etc.

binding default case to some variable in match

  • If you want to bind the default case to a variable, you can do it like this:
fn main() {
    let num = 8;

    match num {
        1 => println!("One"),
        2 | 3 => println!("Two or Three"),
        4..=6 => println!("Between 4 and 6"),
        unmatched => println!("No match found for {}", unmatched), // bind and print value
    }
}

Loops

loop (infinite loop until broken)

let mut count = 0;

loop {
    count += 1;
    println!("count = {}", count);

    if count == 5 {
        break; // Exit the loop
    }
}

while (conditional loop)

let mut num = 3;

while num != 0 {
    println!("{}", num);
    num -= 1;
}
println!("Liftoff!");

for (iterate over a collection or range)

// Over range
for i in 1..=5 { // inclusive range 1 to 5
    println!("{}", i);
}

// Over array
let arr = [10, 20, 30];
for val in arr {
    println!("{}", val);
}

ownership in for loops

  • When iterating over a collection, the ownership of the elements is moved into the loop.
  • If the elements are not Copy, they cannot be used after the loop.
fn main() {
    let arr = [String::from("a"), String::from("b")];

    for val in arr {
        println!("{}", val);
    }
    // println!("{:?}", arr); // Error! arr has been moved
}
  • If you want to borrow instead, use iter() or iter_mut():
let arr = [String::from("a"), String::from("b")];
for val in arr.iter() {
    println!("{}", val); // borrows each element
}
println!("{:?}", arr);

Loop labels (for nested loops)

  • label is of the form 'label_name
  • used to break or continue specific loops in nested structures
'outer: for i in 1..=3 {
    for j in 1..=3 {
        if i * j == 4 {
            break 'outer; // breaks the outer loop
        }
        println!("i = {}, j = {}", i, j);
    }
}