Integer Ranges
Ranges describe integer induction for for loops. Use start..end when the upper bound is excluded and start..=end when it is included. The endpoint type is part of the range; use an explicit integer type when its width matters.
Exclusive and inclusive bounds
Save this complete example as ranges.vx:
fn main(): i32 {
let! exclusive = 0;
for value in 0..5 { exclusive += value; }
let! inclusive = 0;
for value in 0..=5 { inclusive += value; }
$println(exclusive);
$println(inclusive);
if exclusive != 10 || inclusive != 15 { return 1; }
return 0;
}Expected stdout is 10 followed by 15, with exit status 0. Run it with vex run ranges.vx.
For a collection with len elements, 0..len is the usual index interval. It is empty when len is zero. Avoid rewriting it as 0..=len-1: subtracting one from a zero unsigned length is not a valid way to express an empty range.
Direction and step
Use an explicit while loop when the induction requires a custom increment or a descending traversal. Keep the termination test and update together:
fn main(): i32 {
let! even_total = 0;
let! even = 0;
while even < 10 { even_total += even; even += 2; }
let! descending_total = 0;
let! descending = 10;
while descending > 0 { descending_total += descending; descending -= 1; }
$println(even_total);
$println(descending_total);
if even_total != 20 || descending_total != 55 { return 1; }
return 0;
}Expected stdout is 20 followed by 55, exit status 0. The descending example includes 10 and excludes 0. Choose a nonzero update that moves toward the termination bound. Keep the update and bounds representable in their integer type.
Bounded range values
The prelude defines Range<T> and RangeInclusive<T> for bounded values:
let indices: Range<i32> = 0..4;
for index in indices { $println(index); }The fragment prints 0 through 3 when placed inside a function. These are compiler-recognized integer cursors. Their existence does not imply a Rust-style iterator-adapter API: this guide does not provide Range.collect, stepBy, rev, filter, or map recipes without corresponding methods. Use an explicit loop when accumulating or transforming values.
For example, a bounded collection can be constructed with ordinary ownership:
fn main(): i32 {
let! squares = Vec.new<i32>();
for value in 0..4 { squares.push(value * value); }
$println(squares.len());
for value in squares { $println(*value); }
return 0;
}Expected stdout is 4, 0, 1, 4, and 9, each on its own line, with exit status 0. This example uses a borrowed Vec iterator; the vector remains the owner of its elements.
Open-ended syntax
A range with an omitted endpoint is allowed only directly as the iterator of an ordinary for loop in the current language contract. It is not a first-class RangeFrom, RangeTo, or RangeFull object that can be stored and collected later. Bound the work explicitly:
for index in 5.. {
if index == 8 { break; }
$println(index);
}This fragment prints 5, 6, and 7. Do not rely on integer overflow to end an open-ended loop.
Numeric domain
Range induction requires exact integer endpoint types. Floating-point range values such as 0.0..1.0 are not the supported iteration API. For a fixed number of floating samples, use an integer index and compute each sample from that index:
for index in 0..10 {
let sample: f64 = (index as f64) / 10.0;
$println(sample);
}This performs exactly ten iterations. Floating-point rounding still affects each sample; deriving samples from the index avoids accumulating an increment as loop state.
Patterns and slicing are separate uses
An interval in a match arm classifies a value; it does not construct an iterator:
fn grade(score: i32): str {
return match score {
0..60 => "below threshold",
60..=100 => "passing",
_ => "invalid score",
};
}Collection slicing follows the collection's API and bounds policy. A Span<T>.slice(start, end) produces a borrowed view and clamps the requested bounds in the current prelude. It does not allocate or extend the owner's lifetime. See Span and Strings for their different element and byte boundaries.
Custom iterables
A source-defined iterable uses the IntoIterator.iter() and Iterator.next() contracts. Implementing next() alone does not define the .. operator or create range syntax for an arbitrary type. See Contracts and Loops for iteration and item ownership.