[][src]Function alloc_wg::slice::check_range

pub fn check_range<R>(len: usize, range: R) -> Range<usize> where
    R: RangeBounds<usize>, 
🔬 This is a nightly-only experimental API. (slice_check_range)

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

Panics when Index::index would panic:

This example panics
#![feature(slice_check_range)]

std::slice::check_range(3, 2..1);
This example panics
#![feature(slice_check_range)]

std::slice::check_range(3, 1..4);
This example panics
#![feature(slice_check_range)]

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