Struct RangeInclusive

pub struct RangeInclusive<Idx> { pub start: Idx, pub last: Idx }

A range bounded inclusively below and above.

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

Examples

use core::range::RangeInclusive;

assert_eq!(RangeInclusive::from(3..=5), RangeInclusive { start: 3, last: 5 });
assert_eq!(3 + 4 + 5, RangeInclusive::from(3..=5).into_iter().sum());

Edition notes

It is planned that the syntax start..=last 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).

last: Idx

The upper bound of the range (inclusive).

Implementations

impl<Idx: PartialOrd<Idx>> RangeInclusive<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::RangeInclusive;

assert!(!RangeInclusive::from(3..=5).contains(&2));
assert!( RangeInclusive::from(3..=5).contains(&3));
assert!( RangeInclusive::from(3..=5).contains(&4));
assert!( RangeInclusive::from(3..=5).contains(&5));
assert!(!RangeInclusive::from(3..=5).contains(&6));

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

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

Returns true if the range contains no items.

Examples

use core::range::RangeInclusive;

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

The range is empty if either side is incomparable:

use core::range::RangeInclusive;

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

impl<Idx: Step> RangeInclusive<Idx>

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

Creates an iterator over the elements within this range.

Shorthand for .clone().into_iter()

Examples

use core::range::RangeInclusive;

let mut i = RangeInclusive::from(3..=8).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 Distribution<i128> for RangeInclusive<i128>

fn sample(&self, source: &mut impl Rng + ?Sized) -> i128

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<i16> for RangeInclusive<i16>

fn sample(&self, source: &mut impl Rng + ?Sized) -> i16

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<i32> for RangeInclusive<i32>

fn sample(&self, source: &mut impl Rng + ?Sized) -> i32

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<i64> for RangeInclusive<i64>

fn sample(&self, source: &mut impl Rng + ?Sized) -> i64

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<i8> for RangeInclusive<i8>

fn sample(&self, source: &mut impl Rng + ?Sized) -> i8

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<isize> for RangeInclusive<isize>

fn sample(&self, source: &mut impl Rng + ?Sized) -> isize

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<u128> for RangeInclusive<u128>

fn sample(&self, source: &mut impl Rng + ?Sized) -> u128

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<u16> for RangeInclusive<u16>

fn sample(&self, source: &mut impl Rng + ?Sized) -> u16

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<u32> for RangeInclusive<u32>

fn sample(&self, source: &mut impl Rng + ?Sized) -> u32

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<u64> for RangeInclusive<u64>

fn sample(&self, source: &mut impl Rng + ?Sized) -> u64

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<u8> for RangeInclusive<u8>

fn sample(&self, source: &mut impl Rng + ?Sized) -> u8

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl Distribution<usize> for RangeInclusive<usize>

fn sample(&self, source: &mut impl Rng + ?Sized) -> usize

Chooses a random number within the range.

Every possible result value is equally likely. In other words, this operation uses unbiased uniform sampling.

Panics

Panics if the range is empty.

Side-channels

This implementation does not claim to be resistant against side- channel attacks. In particular, the execution time of this operation may leak information about the returned value, and not just the values of the range bounds. While this implementation tries to avoid operations with particularly data-dependent timing (such as divisions), Rust as a language has no facilities for ensuring data-independent timing, voiding all promises about side-channel- freedom.

Examples

A D20 dice roll:

#![feature(random)]

use std::random::{Distribution, SystemRng};
use std::range::RangeInclusive;

let roll = RangeInclusive::from(1..=20).sample(&mut SystemRng);
assert!(1 <= roll && roll <= 20);
if roll == 20 {
    println!("Wow! You achieve writing a sound linked list.");
} else {
    println!("Miri attacks!");
}

impl GetDisjointMutIndex for RangeInclusive<usize>

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

impl SliceIndex<ByteStr> for RangeInclusive<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 RangeInclusive<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 RangeInclusive<A>

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

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

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

impl<Idx: Copy> Copy for RangeInclusive<Idx>

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

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

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

fn assert_fields_are_eq(&self)

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

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

impl<Idx: PartialEq> PartialEq for RangeInclusive<Idx>

fn eq(&self, other: &RangeInclusive<Idx>) -> bool

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

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

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));
# // This test requires unwinding to work.
# // Disable it when unwinding isn't available.
# #[cfg(panic = "unwind")]
# fn main() {
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()));
# }
# #[cfg(not(panic = "unwind"))]
# fn main() {}

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

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

impl<T> RangeBounds<T> for RangeInclusive<&T>

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

impl<T> RangeBounds<T> for RangeInclusive<T>

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

impl<T> SliceIndex<[T]> for RangeInclusive<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]

Auto Trait Implementations

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

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

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

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

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

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

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

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

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

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Printable for RangeInclusive<Idx> where T: Copy + Debug,

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

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

impl<T> SizedTypeProperties for RangeInclusive<Idx>

impl<T, U> Into<U> for RangeInclusive<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 RangeInclusive<Idx> where U: Into<T>,

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

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

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