Control Flow

Explains the general control flow of exolynk scripts.

Rune supports a number of control flow expressions. We will be dedicating this section to describe the most common ones.

return expression

In the previous section we talked about functions. And one of the primary things a function does is return things. The return expression allows for returning from the current function. If used without an argument, the function will return a unit ().

The last statement in a function is known as an implicit return, and will be what the function returns by default unless a return is specified.

fn foo(n) {
    if n < 1 {
        return "less than one";
    }

    "something else"
}

#[test]
fn test_return_exp() {
    assert_eq!(foo(0), "less than one");
    assert_eq!(foo(10), "something else");
}

if expression

If expressions allow you to provide a condition with one or more code branches. When the condition is true, the provided block of code will run. We can add an arbitrary number of else if branches, which allow us to specify many different conditions.

#[test]
pub fn test() {
    let number = 3;

    if number < 5 {
        println!("the number is smaller than 5");
    } else if number == 5 {
        println!("the number is exactly 5");
    } else {
        println!("the number is bigger than 5");
    }
}

match expression

When there are many conditions its better to use a match statement. This will be covered in a later section, but here is a sneak peek:

#[test]
pub fn test() {
    let number = 3;

    match number {
        n if n < 5 => {
            println!("the number is smaller than 5");
        }
        5 => {
            println!("the number is exactly 5");
        }
        n => {
            println!("the number is bigger than 5");
        }
    }
}