Control Flow

if / else🔗

let x = 10
if x > 5 {
    println("big")
} else {
    println("small")
}

// if as expression
let label = if x > 5 { "big" } else { "small" }

match🔗

match is exhaustive — every variant must be covered.

enum Status { Active, Inactive, Pending }

fn describe(s: Status) -> String {
    match s {
        Status::Active => "running",
        Status::Inactive => "stopped",
        Status::Pending => "waiting",
    }
}

With enum data:

enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
}

match shape {
    Shape::Circle(r) => println(f"Circle r={r}"),
    Shape::Rectangle(w, h) => println(f"Rect {w}x{h}"),
}

if let🔗

Matches a single pattern and binds the inner value.

if let Some(value) = maybe_value {
    println(f"Got: {value}")
}

while🔗

let mut i: i32 = 0
while i < 10 {
    println(f"i = {i}")
    i = i + 1
}

for🔗

let items = ["a", "b", "c"]
for item in items {
    println(item)
}

loop / break / continue🔗

let mut count: i32 = 0
loop {
    count = count + 1
    if count == 3 { continue }
    if count == 5 { break }
    println(f"count = {count}")
}

The ? operator🔗

Propagates Err or None from the current function. See Error Handling.