Struct std::ops::Range
[−]
[src]
pub struct Range<Idx> { pub start: Idx, pub end: Idx, }1.0.0
A (half-open) range which is bounded at both ends: { x | start <= x < end }.
Use start..end
(two dots) for its shorthand.
See the contains()
method for its characterization.
Examples
#![feature(iter_arith)] fn main() { assert_eq!((3..5), std::ops::Range{ start: 3, end: 5 }); assert_eq!(3+4+5, (3..6).sum()); let arr = [0, 1, 2, 3]; assert_eq!(arr[ .. ], [0,1,2,3]); assert_eq!(arr[ ..3], [0,1,2 ]); assert_eq!(arr[1.. ], [ 1,2,3]); assert_eq!(arr[1..3], [ 1,2 ]); // Range }#![feature(iter_arith)] fn main() { assert_eq!((3..5), std::ops::Range{ start: 3, end: 5 }); assert_eq!(3+4+5, (3..6).sum()); let arr = [0, 1, 2, 3]; assert_eq!(arr[ .. ], [0,1,2,3]); assert_eq!(arr[ ..3], [0,1,2 ]); assert_eq!(arr[1.. ], [ 1,2,3]); assert_eq!(arr[1..3], [ 1,2 ]); // Range }
Fields
start | The lower bound of the range (inclusive). |
end | The upper bound of the range (exclusive). |
Methods
impl<A> Range<A> where A: Step
fn step_by(self, by: A) -> StepBy<A, Range<A>>
Creates an iterator with the same range, but stepping by the given amount at each iteration.
The resulting iterator handles overflow by stopping.
Examples
#![feature(step_by)] fn main() { for i in (0..10).step_by(2) { println!("{}", i); } }#![feature(step_by)] for i in (0..10).step_by(2) { println!("{}", i); }
This prints:
0
2
4
6
8
impl<Idx> Range<Idx> where Idx: PartialOrd<Idx>
fn contains(&self, item: Idx) -> bool
Examples
#![feature(range_contains)] fn main() { assert!( ! (3..5).contains(2)); assert!( (3..5).contains(3)); assert!( (3..5).contains(4)); assert!( ! (3..5).contains(5)); assert!( ! (3..3).contains(3)); assert!( ! (3..2).contains(3)); }#![feature(range_contains)] fn main() { assert!( ! (3..5).contains(2)); assert!( (3..5).contains(3)); assert!( (3..5).contains(4)); assert!( ! (3..5).contains(5)); assert!( ! (3..3).contains(3)); assert!( ! (3..2).contains(3)); }