LEARN VOID / 06

Collections

Keep related values together.

A growable list, one new score and a total.

collections.voidDownload source ↓
func entry(latest_score: i32) -> i32:
    var scores: [i32] = [10, 20]
    scores.push(latest_score)

    var total = 0
    for score in scores:
        total += score
    return total
Input 5Expected result 35

Start with a list

[i32] is a growable list of integers. The example starts with two scores, 10 and 20. It keeps one task in view: add a new score and calculate the total.

Append a new value

scores.push(latest_score) adds the input at the end of the list. With input 5, the scores are [10, 20, 5].

Visit each item

for score in scores: visits each score and adds it to total. The result is 35. Growable lists share identity when assigned to another variable. Fixed arrays use a size in their type, such as [i32, 3], and copy their value storage. Key/value data has its own dictionary guide.