thp-web/md/learn/flow-control/loops.md

843 B

Loops

For loop

Braces are required.

val numbers = [0, 1, 2, 3]

for number in numbers
{
    print(number)
}

for #(index, number) in numbers.entries()
{
    print("{index} => {number}")
}
val dict = Obj {
    apple: 10,
    banana: 7,
    cherries: 3,
}

for #(key, value) in dict
{
    print("{key} => {value}")
}
for value in collection
{
    if condition
    {
        break
    }
}

While loop

val colors = ["red", "green", "blue"]
val mut index = 0

while index < colors.size()
{
    print("{colors[index]}")
    index += 1
}

Infinite loop

Basically Rust*'s loop.

loop
{
    print("looping")

    if condition
    {
        break
    }
}
  • Rust is a trademark of the Rust Foundation. THP is not affiliated, endorsed or supported by the Rust Foundation.