Skip to main content

Range

Struct Range 

1.98.0 ยท Source
pub struct Range<Idx> {
    pub start: Idx,
    pub end: Idx,
}
Expand description

A (half-open) range bounded inclusively below and exclusively above (start..end).

The range start..end contains all values with start <= x < end. It is empty if start >= end.

ยงExamples

The start..end syntax is a Range:

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, 4];
assert_eq!(arr[ ..  ], [0, 1, 2, 3, 4]);
assert_eq!(arr[ .. 3], [0, 1, 2      ]);
assert_eq!(arr[ ..=3], [0, 1, 2, 3   ]);
assert_eq!(arr[1..  ], [   1, 2, 3, 4]);
assert_eq!(arr[1.. 3], [   1, 2      ]); // This is a `Range`
assert_eq!(arr[1..=3], [   1, 2, 3   ]);

Fieldsยง

ยงstart: Idx

The lower bound of the range (inclusive).

ยงend: Idx

The upper bound of the range (exclusive).

Implementationsยง

Sourceยง

impl<Idx> Range<Idx>
where Idx: PartialOrd,

1.35.0 (const: unstable) ยท Source

pub fn contains<U>(&self, item: &U) -> bool
where Idx: PartialOrd<U>, U: PartialOrd<Idx> + ?Sized,

Returns true if item is contained in the range.

ยงExamples
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));

assert!( (0.0..1.0).contains(&0.5));
assert!(!(0.0..1.0).contains(&f32::NAN));
assert!(!(0.0..f32::NAN).contains(&0.5));
assert!(!(f32::NAN..1.0).contains(&0.5));
1.47.0 (const: unstable) ยท Source

pub fn is_empty(&self) -> bool
where Idx: PartialOrd,

Returns true if the range contains no items.

ยงExamples
assert!(!(3..5).is_empty());
assert!( (3..3).is_empty());
assert!( (3..2).is_empty());

The range is empty if either side is incomparable:

assert!(!(3.0..5.0).is_empty());
assert!( (3.0..f32::NAN).is_empty());
assert!( (f32::NAN..5.0).is_empty());

Trait Implementationsยง

1.0.0 (const: unstable) ยท Sourceยง

impl<Idx> Clone for Range<Idx>
where Idx: Clone,

Sourceยง

fn clone(&self) -> Range<Idx> โ“˜

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl<T> CloneFromCell for Range<T>
where T: CloneFromCell,

1.0.0 ยท Sourceยง

impl<Idx> Debug for Range<Idx>
where Idx: Debug,

Sourceยง

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl<Idx> Default for Range<Idx>
where Idx: Default,

Sourceยง

fn default() -> Range<Idx> โ“˜

Returns the โ€œdefault valueโ€ for a type. Read more
1.0.0 ยท Sourceยง

impl<A> DoubleEndedIterator for Range<A>
where A: Step,

Sourceยง

fn next_back(&mut self) -> Option<A>

Removes and returns an element from the end of the iterator. Read more
Sourceยง

fn nth_back(&mut self, n: usize) -> Option<A>

Returns the nth element from the end of the iterator. Read more
Sourceยง

fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>>

๐Ÿ”ฌThis is a nightly-only experimental API. (iter_advance_by #77404)
Advances the iterator from the back by n elements. Read more
Sourceยง

fn next_chunk_back<const N: usize>( &mut self, ) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>
where Self: Sized,

๐Ÿ”ฌThis is a nightly-only experimental API. (iter_next_chunk #98326)
Advances from the back of the iterator and returns an array containing the next N values in sequence. Read more
1.27.0 (const: unstable) ยท Sourceยง

fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B>,

This is the reverse version of Iterator::try_fold(): it takes elements starting from the back of the iterator. Read more
1.27.0 (const: unstable) ยท Sourceยง

fn rfold<B, F>(self, init: B, f: F) -> B
where Self: Sized, F: FnMut(B, Self::Item) -> B,

An iterator method that reduces the iteratorโ€™s elements to a single, final value, starting from the back. Read more
1.27.0 (const: unstable) ยท Sourceยง

fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Searches for an element of an iterator from the back that satisfies a predicate. Read more
1.0.0 ยท Sourceยง

impl<Idx> Eq for Range<Idx>
where Idx: Eq,

1.0.0 ยท Sourceยง

impl ExactSizeIterator for Range<usize>

1.0.0 ยท Sourceยง

fn len(&self) -> usize

Returns the exact remaining length of the iterator. Read more
Sourceยง

fn is_empty(&self) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (exact_size_is_empty #35428)
Returns true if the iterator is empty. Read more
1.0.0 ยท Sourceยง

impl ExactSizeIterator for Range<u8>

1.0.0 ยท Sourceยง

fn len(&self) -> usize

Returns the exact remaining length of the iterator. Read more
Sourceยง

fn is_empty(&self) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (exact_size_is_empty #35428)
Returns true if the iterator is empty. Read more
1.0.0 ยท Sourceยง

impl ExactSizeIterator for Range<u16>

1.0.0 ยท Sourceยง

fn len(&self) -> usize

Returns the exact remaining length of the iterator. Read more
Sourceยง

fn is_empty(&self) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (exact_size_is_empty #35428)
Returns true if the iterator is empty. Read more
1.0.0 ยท Sourceยง

impl ExactSizeIterator for Range<isize>

1.0.0 ยท Sourceยง

fn