Ranges

A range describes an interval between two bounds. It is written with the .. and ..= operators and used mainly to drive a for loop or to take a sub-slice of an array. A range is an ordinary value — it can be stored in a variable and its bounds read back.

for i in 0..5 {
    Print(i);   // 0, 1, 2, 3, 4
}

The Six Forms

The start is inclusive; whether the end is included depends on the operator — .. excludes it (half-open), ..= includes it. Either bound may be omitted:

SyntaxMeaningType
a..ba up to, excluding bRange
a..=ba up to, including bRangeInclusive
a..a onward, no upper boundRangeFrom
..bup to, excluding bRangeTo
..=bup to, including bRangeToInclusive
..everythingRangeFull

The bound type parameterises the range, so 0..10 has type Range<int> and 'a'..='z' has type RangeInclusive<char8>.

let count = 0..10;    // Range<int> — 0,1,…,9
let upto  = 0..=10;   // RangeInclusive<int> — 0,1,…,10

Reading the Bounds

A range that carries a bound exposes it as a field: .start for the lower bound and .end for the upper. The forms without that bound do not have the field.

let r = 5..12;
let lo = r.start;   // 5
let hi = r.end;     // 12

.., ..b, and ..=b have no .start; .., a.. have no .end.

What Ranges Are For

UseExample
Iterating a counted loopfor i in 0..n
Sub-slicing an array or slicedata[1..4]

Both are covered in Using Ranges.

See Also