Loops

Explains how variables behave in exolynk scripts.

Loops are a fundamental building block common to many programming languages. This is no exception in Rune. Loops allow you to execute a block of code until a specific condition is reached, which can be a powerful tool for accomplishing programming tasks.

Every loop documented in this section can be terminated early using the break keyword. When Rune encounters a break, it will immediately jump out of the loop it is currently in and continue running right after it.

loop

The loop keyword builds the most fundamental form of loop in Rune. One that repeats unconditionally forever, until it is exited using another control flow operator like a break or a return.

#[test]
pub fn test() {
    let counter = 0;

    let total = loop {
        counter = counter + 1;

        if counter > 10 {
            break counter;
        }
    };

    assert_eq!(11, total);
}

while

This function loops until the condition after the while keyword is changing from true to false.

#[test]
pub fn test() {
    let value = 0;

    while value < 100 {
        if value >= 50 {
            break;
        }

        value = value + 1;
    }

    assert_eq!(value, 50);
}

for loop

The for loop goes through an range of numbers. In this case from 5 to 10 and stores the actual number into the given variable.

#[test]
pub fn test() {
    for x in 5..10 {
        assert!(x < 10);
        assert!(x >= 5);
    }
}

With the same syntax you can also loop over an array/list.

#[test]
pub fn test() {
    let res = 0;
    for x in [5, 10, 15, 20] {
        res += x;
    }
    assert_eq!(res, 50);
}

iterator

Vectors and other types can provide an iter() function to expose an iterator. These iterators allow to perform action in a function like syntax.

#[test]
pub fn test() {
    let array = [5, 10, 15, 20];

    // Check if a element is inside the list
    assert!(array.iter().any(|x| x==20));

    // Add 5 to all elements
    assert_eq!(70, array.iter().map(|x| x + 5).sum::<i64>());

    // Only entries bigger than 10
    assert_eq!([15, 20], array.iter().filter(|x| x > 10).collect::<Vec>());
}

You can find more functions for iter within the script api under ::std::iter::Iterator.