1.0.0[][src]Struct std::ops::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 x >= start and x < end. It is empty unless start < end.

Examples

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    ]);  // Range
assert_eq!(arr[1..=3], [  1,2,3  ]);Run

Fields

start: Idx

The lower bound of the range (inclusive).

end: Idx

The upper bound of the range (exclusive).

Methods

impl<Idx> Range<Idx> where
    Idx: PartialOrd<Idx>, 
[src]

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

Returns true if item is contained in the range.

Examples

use std::f32;

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));Run

pub fn is_empty(&self) -> bool[src]

🔬 This is a nightly-only experimental API. (range_is_empty #48111)

recently added

Returns true if the range contains no items.

Examples

#![feature(range_is_empty)]

assert!(!(3..5).is_empty());
assert!( (3..3).is_empty());
assert!( (3..2).is_empty());Run

The range is empty if either side is incomparable:

#![feature(range_is_empty)]

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

Trait Implementations

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

type Output = [T]

The output type returned by methods.

impl SliceIndex<str> for Range<usize>1.20.0[src]

Implements substring slicing with syntax &self[begin .. end] or &mut self[begin .. end].

Returns a slice of the given string from the byte range [begin, end).

This operation is O(1).

Prior to 1.20.0, these indexing operations were still supported by direct implementation of Index and IndexMut.

Panics

Panics if begin or end does not point to the starting byte offset of a character (as defined by is_char_boundary), if begin > end, or if end > len.

Examples

let s = "Löwe 老虎 Léopard";
assert_eq!(&s[0 .. 1], "L");

assert_eq!(&s[1 .. 9], "öwe 老");

// these will panic:
// byte 2 lies within `ö`:
// &s[2 ..3];

// byte 8 lies within `老`
// &s[1 .. 8];

// byte 100 is outside the string
// &s[3 .. 100];Run

type Output = str

The output type returned by methods.

impl<A> FusedIterator for Range<A> where
    A: Step
1.26.0[src]

impl<Idx> Eq for Range<Idx> where
    Idx: Eq
[src]

impl<Idx> Debug for Range<Idx> where
    Idx: Debug
[src]

impl<Idx> PartialEq<Range<Idx>> for Range<Idx> where
    Idx: PartialEq<Idx>, 
[src]

impl<Idx> Hash for Range<Idx> where
    Idx: Hash
[src]

impl ExactSizeIterator for Range<isize>[src]

impl ExactSizeIterator for Range<i32>[src]

impl ExactSizeIterator for Range<i16>[src]

impl ExactSizeIterator for Range<usize>[src]

impl ExactSizeIterator for Range<u16>[src]

impl ExactSizeIterator for Range<i8>[src]

impl ExactSizeIterator for Range<u32>[src]

impl ExactSizeIterator for Range<u8>[src]

impl TrustedLen for Range<i32>[src]

impl TrustedLen for Range<u16>[src]

impl TrustedLen for Range<i64>[src]

impl TrustedLen for Range<usize>[src]

impl TrustedLen for Range<u8>[src]

impl TrustedLen for Range<i128>[src]

impl TrustedLen for Range<u64>[src]

impl TrustedLen for Range<isize>[src]

impl TrustedLen for Range<i8>[src]

impl TrustedLen for Range<u32>[src]

impl TrustedLen for Range<u128>[src]

impl TrustedLen for Range<i16>[src]

impl<'_, T> RangeBounds<T> for Range<&'_ T>1.28.0[src]

impl<T> RangeBounds<T> for Range<T>1.28.0[src]

impl<Idx> Clone for Range<Idx> where
    Idx: Clone
[src]

impl<A> DoubleEndedIterator for Range<A> where
    A: Step
[src]

impl<A> Iterator for Range<A> where
    A: Step
[src]

type Item = A

The type of the elements being iterated over.

impl IndexMut<Range<usize>> for String1.3.0[src]

impl Index<Range<usize>> for String[src]

type Output = str

The returned type after indexing.

Auto Trait Implementations

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

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

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

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

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

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.