This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/_archive/rhai_engine/rhaibook/language/do.md
2025-04-04 08:28:07 +02:00

1.5 KiB

Do Loop

{{#include ../links.md}}

do loops have two opposite variants: do ... while and do ... until.

Like the [while] loop, continue can be used to skip to the next iteration, by-passing all following statements; break can be used to break out of the loop unconditionally.


`do` loops can be disabled via [`Engine::set_allow_looping`][options].
let x = 10;

do {
    x -= 1;
    if x < 6 { continue; }  // skip to the next iteration
    print(x);
    if x == 5 { break; }    // break out of do loop
} while x > 0;


do {
    x -= 1;
    if x < 6 { continue; }  // skip to the next iteration
    print(x);
    if x == 5 { break; }    // break out of do loop
} until x == 0;

Do Expression

Like Rust, do statements can also be used as expressions.

The break statement takes an optional expression that provides the return value.

The default return value of a do expression is [()].


Loop expressions can be disabled via [`Engine::set_allow_loop_expressions`][options].
let x = 0;

// 'do' can be used just like an expression
let result = do {
    if is_magic_number(x) {
        // if the 'do' loop breaks here, return a specific value
        break get_magic_result(x);
    }

    x += 1;

    // ... if the 'do' loop exits here, the return value is ()
} until x >= 100;

if result == () {
    print("Magic number not found!");
} else {
    print(`Magic result = ${result}!`);
}