core::slice::check_range - Rust
Description: API documentation for the Rust `check_range` fn in crate `core`.
Keywords: rust, rustlang, rust-lang, check_range

# [−][src]Function core::slice::check_range

###

    pub fn check_range<R: RangeBounds<usize>>(len: usize, range: R) -> Range<usize>ⓘNotable traits for Range<A>impl<A: Step> Iterator for Range<A>    type Item = A;

🔬 This is a nightly-only experimental API. (`slice_check_range` #76393)

Performs bounds-checking of the given range. The returned `Range` is safe to pass to `get_unchecked` and `get_unchecked_mut` for slices of the given length.

# Panics

Panics if the range is out of bounds.

# Examples

    #![feature(slice_check_range)]
    use std::slice;

    let v = [10, 40, 30];
    assert_eq!(1..2, slice::check_range(v.len(), 1..2));
    assert_eq!(0..2, slice::check_range(v.len(), ..2));
    assert_eq!(1..3, slice::check_range(v.len(), 1..));

Run

Panics when `Index::index` would panic:

ⓘThis example panics

    #![feature(slice_check_range)]

    std::slice::check_range(3, 2..1);

Run
ⓘThis example panics

    #![feature(slice_check_range)]

    std::slice::check_range(3, 1..4);

Run
ⓘThis example panics

    #![feature(slice_check_range)]

    std::slice::check_range(3, 1..=usize::MAX);

Run