Function konst::slice::slice_range

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

A const equivalent of &slice[start..end].

If start >= end or slice.len() < start , this returns an empty slice.

If slice.len() < end, this returns the slice from start.

§Alternatives

For a const equivalent of &slice[start..] there’s slice_from.

For a const equivalent of &slice[..end] there’s slice_up_to.

§Example

use konst::slice::slice_range;

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

const TWO: &[u16] = slice_range(FIBB, 2, 4);
const FOUR: &[u16] = slice_range(FIBB, 4, 7);
const NONE: &[u16] = slice_range(FIBB, 0, 0);
const ALL: &[u16] = slice_range(FIBB, 0, 1000);

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