Skip to main content

RangeInclusive

Struct RangeInclusive 

1.26.0 · Source
pub struct RangeInclusive<Idx> { /* private fields */ }
Expand description

A range bounded inclusively below and above (start..=end).

The RangeInclusive start..=end contains all values with x >= start and x <= end. It is empty unless start <= end.

This iterator is fused, but the specific values of start and end after iteration has finished are unspecified other than that .is_empty() will return true once no more values will be produced.

§Examples

The start..=end syntax is a RangeInclusive:

assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5));
assert_eq!(3 + 4 + 5, (3..=5).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      ]);
assert_eq!(arr[1..=3], [   1, 2, 3   ]); // This is a `RangeInclusive`

Implementations§

Source§

impl<Idx> RangeInclusive<Idx>

1.27.0 (const: 1.32.0) · Source

pub const fn new(start: Idx, end: Idx) -> Self

Creates a new inclusive range. Equivalent to writing start..=end.

§Examples
use std::ops::RangeInclusive;

assert_eq!(3..=5, RangeInclusive::new(3, 5));
1.27.0 (const: 1.32.0) · Source

pub const fn start(&self) -> &Idx

Returns the lower bound of the range (inclusive).

When using an inclusive range for iteration, the values of start() and end() are unspecified after the iteration ended. To determine whether the inclusive range is empty, use the is_empty() method instead of comparing start() > end().

Note: the value returned by this method is unspecified after the range has been iterated to exhaustion.

§Examples
assert_eq!((3..=5).start(), &3);
1.27.0 (const: 1.32.0) · Source

pub const fn end(&self) -> &Idx

Returns the upper bound of the range (inclusive).

When using an inclusive range for iteration, the values of start() and end() are unspecified after the iteration ended. To determine whether the inclusive range is empty, use the is_empty() method instead of comparing start() > end().

Note: the value returned by this method is unspecified after the range has been iterated to exhaustion.

§Examples
assert_eq!((3..=5).end(), &5);
1.27.0 (const: unstable) · Source

pub fn into_inner(self) -> (Idx, Idx)

Destructures the RangeInclusive into (lower bound, upper (inclusive) bound).

Note: the value returned by this method is unspecified after the range has been iterated to exhaustion.

§Examples
assert_eq!((3..=5).into_inner(), (3, 5));
Source§

impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx>

1.35.0 (const: unstable) · Source

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

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..=5).contains(&6));

assert!( (3..=3).contains(&3));
assert!(!(3..=2).contains(&3));

assert!( (0.0..=1.0).contains(&1.0));
assert!(!(0.0..=1.0).contains(&f32::NAN));
assert!(!(0.0..=f32::NAN).contains(&0.0));
assert!(!(f32::NAN..=1.0).contains(&1.0));

This method always returns false after iteration has finished:

let mut r = 3..=5;
assert!(r.contains(&3) && r.contains(&5));
for _ in r.by_ref() {}
// Precise field values are unspecified here
assert!(!r.contains(&3) && !r.contains(&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());

This method returns true after iteration has finished:

let mut r = 3..=5;
for _ in r.by_ref() {}
// Precise field values are unspecified here
assert!(r.is_empty());

Trait Implementations§

1.26.0 · Source§

impl<Idx: Clone> Clone for RangeInclusive<Idx>

Source§

fn clone(&self) -> RangeInclusive<Idx>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
1.26.0 · Source§

impl<Idx: Debug> Debug for RangeInclusive<Idx>

Source§

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

Formats the value using the given formatter. Read more
1.26.0 · Source§

impl<A: Step> DoubleEndedIterator for RangeInclusive<A>

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 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
Source§

fn rfold<AAA, FFF>(self, init: AAA, fold: FFF) -> AAA
where FFF: FnMut(AAA, Self::Item) -> AAA,

An iterator method that reduces the iterator’s elements to a single, final value, starting from the back. 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
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
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.26.0 (const: unstable) · Source§

impl<Idx: Eq> Eq for RangeInclusive<Idx>

1.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<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.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<i8>

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.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<NonZero<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.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<NonZero<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.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<NonZero<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.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<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.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<i16>

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.95.0 (const: unstable) · Source§

impl<T> From<RangeInclusive<T>> for RangeInclusive<T>

Source§

fn from(value: RangeInclusive<T>) -> Self

Converts to this type from the input type.
1.95.0 (const: unstable) · Source§

impl<T> From<RangeInclusive<T>> for RangeInclusive<T>

Source§

fn from(value: RangeInclusive<T>) -> Self

Converts from a legacy range to a non-legacy range, potentially panicking.

§Panics

If the legacy range iterator has been exhausted, this function will either panic or return an empty range.

§Examples
use core::range::legacy;
use core::range::RangeInclusive;

let single: legacy::RangeInclusive<i32> = 0..=1;
let single = RangeInclusive::from(single);
assert_eq!((single.start, single.last), (0, 1));

let empty: legacy::RangeInclusive<i32> = 0..=0;
let empty = RangeInclusive::from(empty);
assert_eq!((empty.start, empty.last), (0, 0));
use core::range::legacy;
use core::range::RangeInclusive;
use std::panic::catch_unwind;

let mut exhausted: legacy::RangeInclusive<i32> = 0..=0;
exhausted.next();
let result = catch_unwind(|| RangeInclusive::from(exhausted));
// The `from` call either panicked or returned an empty range.
assert!(result.is_err() || result.is_ok_and(|range| range.is_empty()));
1.26.0 · Source§

impl<A: Step> FusedIterator for RangeInclusive<A>

Source§

impl GetDisjointMutIndex for RangeInclusive<usize>

Source§

fn is_in_bounds(&self, len: usize) -> bool

🔬This is a nightly-only experimental API. (get_disjoint_mut_helpers)
Returns true if self is in bounds for len slice elements.
Source§

fn is_overlapping(&self, other: &Self) -> bool

🔬This is a nightly-only experimental API. (get_disjoint_mut_helpers)
Returns true if self overlaps with other. Read more
1.26.0 · Source§

impl<Idx: Hash> Hash for RangeInclusive<Idx>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
where Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> IntoBounds<T> for RangeInclusive<T>