Function konst::slice::slice_range[][src]

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.

Performance

If the “constant_time_slice” feature is disabled, thich takes linear time to remove the leading and trailing elements, proportional to start + (slice.len() - end).

If the “constant_time_slice” feature is enabled, it takes constant time to run, but uses a few nightly features.

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