Function konst::slice::get_range

source ·
pub const fn get_range<T>(slice: &[T], start: usize, end: usize) -> Option<&[T]>
Expand description

A const equivalent of slice.get(start..end).

§Alternatives

For a const equivalent of slice.get(start..) there’s get_from.

For a const equivalent of slice.get(..end) there’s get_up_to.

§Example

use konst::slice;

const FIBB: &[u16] = &[3, 5, 8, 13, 21, 34, 55, 89];

const TWO: Option<&[u16]> = slice::get_range(FIBB, 2, 4);
const FOUR: Option<&[u16]> = slice::get_range(FIBB, 4, 7);
const ALL: Option<&[u16]> = slice::get_range(FIBB, 0, 8);
const EMPTY: Option<&[u16]> = slice::get_range(FIBB, 0, 0);
const NONE: Option<&[u16]> = slice::get_range(FIBB, 0, 1000);

assert_eq!(TWO, Some(&[8, 13][..]));
assert_eq!(FOUR, Some(&[21, 34, 55][..]));
assert_eq!(ALL, Some(FIBB));
assert_eq!(EMPTY, Some(&[][..]));
assert_eq!(NONE, None);