Struct Range

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

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

The Range 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

use core::range::Range;

assert_eq!(Range::from(3..5), Range { start: 3, end: 5 });
assert_eq!(3 + 4 + 5, Range::from(3..6).into_iter().sum());

Edition notes

It is planned that the syntax start..end will construct this type in a future edition, but it does not do so today.

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

use core::range::Range;

assert!(!Range::from(3..5).contains(&2));
assert!( Range::from(3..5).contains(&3));
assert!( Range::from(3..5).contains(&4));
assert!(!Range::from(3..5).contains(&5));

assert!(!Range::from(3..3).contains(&3));
assert!(!Range::from(3..2).contains(&3));

assert!( Range::from(0.0..1.0).contains(&0.5));
assert!(!Range::from(0.0..1.0).contains(&f32::NAN));
assert!(!Range::from(0.0..f32::NAN).contains(&0.5));
assert!(!Range::from(f32::NAN..1.0).contains(&0.5));
const fn is_empty(&self) -> bool
where
    Idx: ~const PartialOrd,

Returns true if the range contains no items.

Examples

use core::range::Range;

assert!(!Range::from(3..5).is_empty());
assert!( Range::from(3..3).is_empty());
assert!( Range::from(3..2).is_empty());

The range is empty if either side is incomparable:

use core::range::Range;

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

impl<Idx: Step> Range<Idx>

fn iter(&self) -> RangeIter<Idx>

Creates an iterator over the elements within this range.

Shorthand for .clone().into_iter()

Examples

use core::range::Range;

let mut i = Range::from(3..9).iter().map(|n| n*n);
assert_eq!(i.next(), Some(9));
assert_eq!(i.next(), Some(16));
assert_eq!(i.next(), Some(25));

Trait Implementations

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<A: Step> IntoIterator for Range<A>

type Item = A;
type IntoIter = RangeIter<A>;
fn into_iter(self) -> Self::IntoIter

impl<Idx: Copy> Copy for Range<Idx>

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

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

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 Eq> Eq for Range<Idx>

fn assert_fields_are_eq(&self)

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

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

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>

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<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> Printable for Range<Idx> where T: Copy + Debug,

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>