Function std::slice::range
pub fn range<R>(range: R, bounds: RangeTo<usize>) -> Range<usize>ⓘNotable traits for Range<A>impl<A> Iterator for Range<A> where A: Step, type Item = A; where R: RangeBounds<usize>,
Performs bounds-checking of a range.
This method is similar to Index::index
for slices, but it returns a Range
equivalent to range
. You can use this method to turn any range into start
and end
values.
bounds
is the range of the slice to use for bounds-checking. It should be a RangeTo
range that ends at the length of the slice.
The returned Range
is safe to pass to slice::get_unchecked
and slice::get_unchecked_mut
for slices with the given range.
Panics
Panics if range
would be out of bounds.
Examples
#![feature(slice_range)] use std::slice; let v = [10, 40, 30]; assert_eq!(1..2, slice::range(1..2, ..v.len())); assert_eq!(0..2, slice::range(..2, ..v.len())); assert_eq!(1..3, slice::range(1.., ..v.len()));
Panics when Index::index
would panic:
ⓘ This example panics
#![feature(slice_range)] use std::slice; slice::range(2..1, ..3);
ⓘ This example panics
#![feature(slice_range)] use std::slice; slice::range(1..4, ..3);
ⓘ This example panics
#![feature(slice_range)] use std::slice; slice::range(1..=usize::MAX, ..3);
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/slice/fn.range.html