Range expressions
Syntax
RangeExpression :
RangeExpr
| RangeFromExpr
| RangeToExpr
| RangeFullExprRangeExpr :
Expression..
ExpressionRangeFromExpr :
Expression..
RangeToExpr :
..
ExpressionRangeFullExpr :
..
The ..
operator will construct an object of one of the std::ops::Range
(or
core::ops::Range
) variants, according to the following table:
Production | Syntax | Type | Range |
---|---|---|---|
RangeExpr | start.. end | std::ops::Range | start ≤ x < end |
RangeFromExpr | start.. | std::ops::RangeFrom | start ≤ x |
RangeToExpr | .. end | std::ops::RangeTo | x < end |
RangeFullExpr | .. | std::ops::RangeFull | - |
Examples:
# #![allow(unused_variables)] #fn main() { 1..2; // std::ops::Range 3..; // std::ops::RangeFrom ..4; // std::ops::RangeTo ..; // std::ops::RangeFull #}
The following expressions are equivalent.
# #![allow(unused_variables)] #fn main() { let x = std::ops::Range {start: 0, end: 10}; let y = 0..10; assert_eq!(x, y); #}
Ranges can be used in for
loops:
# #![allow(unused_variables)] #fn main() { for i in 1..11 { println!("{}", i); } #}