docs / Getting Started / Control Flow

Control Flow

if / else

fn classify(n int) string
    if n > 0
        return "positive"
    else if n < 0
        return "negative"
    else
        return "zero"

unless

The inverse of if — runs the block when the condition is false:

unless list.is_empty()
    print(f"List has {list.len()} items")

Loops

// For loop with range
for i in 0..10
    print(f"{i}")

// For loop over collection
for item in items
    print(f"{item}")

// While loop
var n = 10
while n > 0
    print(f"{n}")
    n -= 1

// Infinite loop
forever
    let input = read_line()
    if input == "quit"
        break

Miri supports while, until, do-while, forever, and for..in loops. All loop types support inline syntax with a colon: for x in 1..10: print(x).