Rust Range Expression
The following are some range expressions supported in the Rust language:
- RangeExpr
- Range From
- Range To
- Range Full
- Range Inclusive
- Range To Inclusive
Range
This is defined as a half-open range expression. The range operator will iterate over the items specified from the start to the end offset in this expression.
The syntax is as shown:
Consider the example code shown below:
for i in 1..5 {
println!("{}", i);
}
}
The code above will iterate from the values 1 to 5, exclusive of the end offset value. The resulting value is as shown:
1
2
3
4
Range From
Another common range expression ranges from a specific starting point to the end of the iterator.
The syntax is as shown:
Range To
The range to expression is the opposite of the range from expression. In this case, the range runs from the beginning to a specified offset value.
The syntax is as shown:
Range Full
A range full allows you to range from start to end. The syntax is as shown:
Range Inclusive
To range from a specified start and end, you can use the range inclusive expression, inclusive of the high value.
The syntax is as shown:
Range To Inclusive
To range from the start to a specific offset and include the high value, you can use the range to expression.
The syntax is as shown:
Closing
This is a short descriptive article on various range expressions in the Rust programming language. Consider the docs for more.