Struct Range

pub struct Range<Idx> { pub start: Idx, pub end: Idx }

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.

Note that this type is not suited to represent all possible ranges. For example, Range<u8> cannot represent the range that covers all of u8. Use [(Bound<T>, Bound<T>)][Bound] if you need a type that can store an arbitrary range.

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

impl<Idx: PartialOrd<Idx>> Range<Idx>

const fn contains<U>(&self, item: &U) -> bool
where
    Idx: ~const PartialOrd<U>,
    U: ?Sized + ~const 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..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));
const fn is_empty(&self) -> bool
where
    Idx: ~const PartialOrd<Idx>,

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

impl ExactSizeIterator for Range<NonZero<u16>>

impl ExactSizeIterator for Range<NonZero<u8>>

impl ExactSizeIterator for Range<NonZero<usize>>

impl ExactSizeIterator for Range<i16>

impl ExactSizeIterator for Range<i32>

impl ExactSizeIterator for Range<i8>

impl ExactSizeIterator for Range<isize>

impl ExactSizeIterator for Range<u16>

impl ExactSizeIterator for Range<u32>

impl ExactSizeIterator for Range<u8>

impl ExactSizeIterator for Range<usize>

impl GetDisjointMutIndex for Range<usize>

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

impl SliceIndex<ByteStr> for Range<usize>

type Output = ByteStr;
fn get(self, slice: &ByteStr) -> Option<&Self::Output>
fn get_mut(self, slice: &mut ByteStr) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const ByteStr) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut ByteStr) -> *mut Self::Output
fn index(self, slice: &ByteStr) -> &Self::Output
fn index_mut(self, slice: &mut ByteStr) -> &mut Self::Output

impl SliceIndex<str> for Range<usize>

type Output = str;
fn get(self, slice: &str) -> Option<&Self::Output>
fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output
fn index(self, slice: &str) -> &Self::Output
fn index_mut(self, slice: &mut str) -> &mut Self::Output

impl SpecRangeSetup<Range<u16>> for Range<u16>

fn setup(r: Range<u16>, step: usize) -> Range<u16>

impl SpecRangeSetup<Range<u32>> for Range<u32>

fn setup(r: Range<u32>, step: usize) -> Range<u32>

impl SpecRangeSetup<Range<u64>> for Range<u64>

fn setup(r: Range<u64>, step: usize) -> Range<u64>

impl SpecRangeSetup<Range<u8>> for Range<u8>

fn setup(r: Range<u8>, step: usize) -> Range<u8>

impl SpecRangeSetup<Range<usize>> for Range<usize>

fn setup(r: Range<usize>, step: usize) -> Range<usize>

impl TrustedRandomAccess for Range<NonZero<u16>>

impl TrustedRandomAccess for Range<NonZero<u32>>

impl TrustedRandomAccess for Range<NonZero<u64>>

impl TrustedRandomAccess for Range<NonZero<u8>>

impl TrustedRandomAccess for Range<NonZero<usize>>

impl TrustedRandomAccess for Range<i16>

impl TrustedRandomAccess for Range<i32>

impl TrustedRandomAccess for Range<i64>

impl TrustedRandomAccess for Range<i8>

impl TrustedRandomAccess for Range<isize>

impl TrustedRandomAccess for Range<u16>

impl TrustedRandomAccess for Range<u32>

impl TrustedRandomAccess for Range<u64>

impl TrustedRandomAccess for Range<u8>

impl TrustedRandomAccess for Range<usize>

impl TrustedRandomAccessNoCoerce for Range<NonZero<u16>>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<NonZero<u32>>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<NonZero<u64>>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<NonZero<u8>>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<NonZero<usize>>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<i16>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<i32>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<i64>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<i8>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<isize>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<u16>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<u32>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<u64>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<u8>

const MAY_HAVE_SIDE_EFFECT: bool = false;

impl TrustedRandomAccessNoCoerce for Range<usize>

const MAY_HAVE_SIDE_EFFECT: bool = false;

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

impl<A: TrustedStep> TrustedLen for Range<A>

impl<A: ~const Step> DoubleEndedIterator for Range<A>

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

impl<A: ~const Step> Iterator for Range<A>

type Item = A;
fn next(&mut self) -> Option<A>
fn size_hint(&self) -> (usize, Option<usize>)
fn count(self) -> usize
fn nth(&mut self, n: usize) -> Option<A>
fn last(self) -> Option<A>
fn min(self) -> Option<A>
where
    A: Ord,
fn max(self) -> Option<A>
where
    A: Ord,
fn is_sorted(self) -> bool
where
    Self: ,
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
where
    Self: TrustedRandomAccessNoCoerce,

impl<A: ~const Step> RangeIteratorImpl for Range<A>

type Item = A;
fn spec_next(&mut self) -> Option<A>
fn spec_nth(&mut self, n: usize) -> Option<A>
fn spec_advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn spec_next_back(&mut self) -> Option<A>
fn spec_nth_back(&mut self, n: usize) -> Option<A>
fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>>

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

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

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

fn assert_fields_are_eq(&self)

impl<Idx: Hash> Hash for Range<Idx>

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

impl<Idx: PartialEq> StructuralPartialEq for Range<Idx>

impl<Idx: ~const Clone> Clone for Range<Idx>

fn clone(&self) -> Self

impl<Idx: ~const Default> Default for Range<Idx>

fn default() -> Self

impl<Idx: ~const PartialEq> PartialEq for Range<Idx>

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

impl<T> AsLegacyRange<T> for Range<T>

fn as_legacy_range(&self) -> &Range<T>
fn as_legacy_range_mut(&mut self) -> &mut Range<T>

impl<T> From<Range<T>> for Range<T>

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

impl<T> IntoBounds<T> for Range<T>

fn into_bounds(self) -> (Bound<T>, Bound<T>)

impl<T> RangeBounds<T> for Range<&T>

fn start_bound(&self) -> Bound<&T>
fn end_bound(&self) -> Bound<&T>

impl<T> RangeBounds<T> for Range<T>

fn start_bound(&self) -> Bound<&T>
fn end_bound(&self) -> Bound<&T>

impl<T> SliceIndex<[T]> for Range<usize>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&[T]>
fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T]
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T]
fn index(self, slice: &[T]) -> &[T]
fn index_mut(self, slice: &mut [T]) -> &mut [T]

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

impl<T: ~const TrustedStep> RangeIteratorImpl for Range<T>

fn spec_next(&mut self) -> Option<T>
fn spec_nth(&mut self, n: usize) -> Option<T>
fn spec_advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn spec_next_back(&mut self) -> Option<T>
fn spec_nth_back(&mut self, n: usize) -> Option<T>
fn spec_advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>>

Auto Trait Implementations

impl<Idx> Freeze for Range<Idx> where Idx: Freeze + Freeze,

impl<Idx> RefUnwindSafe for Range<Idx> where Idx: RefUnwindSafe + RefUnwindSafe,

impl<Idx> Send for Range<Idx> where Idx: Send + Send,

impl<Idx> Sync for Range<Idx> where Idx: Sync + Sync,

impl<Idx> Unpin for Range<Idx> where Idx: Unpin + Unpin,

impl<Idx> UnsafeUnpin for Range<Idx> where Idx: UnsafeUnpin + UnsafeUnpin,

impl<Idx> UnwindSafe for Range<Idx> where Idx: UnwindSafe + UnwindSafe,

Blanket Implementations

impl<I> IntoIterator for Range<Idx> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

impl<T> Any for Range<Idx> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Range<Idx> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Range<Idx> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

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

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Range<Idx>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for Range<Idx> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for Range<Idx>

impl<T, U> Into<U> for Range<Idx> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Range<Idx> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Range<Idx> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>