while-let Expression

The while-let expression evaluates the expression on the right of <- in the condition. If the value matches the pattern on the left of <-, the loop body is executed and the process is repeated. If the pattern matching fails, the loop ends and the code following the while-let expression continues to execute. Example:

import std.random.*

// This function simulates receiving data in communication, which may fail.
func recv(): Option<UInt8> {
    let number = Random().nextUInt8()
    if (number < 128) {
        return Some(number)
    }
    return None
}

main() {
    // Simulate a loop to receive communication data. The loop ends if it fails.
    while (let Some(data) <- recv()) {
        println(data)
    }
    println("receive failed")
}

After the preceding program is run, the possible output is as follows:

73
94
receive failed