Function konst::slice::slice_up_to[][src]

pub const fn slice_up_to<T>(slice: &[T], len: usize) -> &[T]
Expand description

A const equivalent of &slice[..len].

If slice.len() < len, this simply returns slice back.

Performance

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

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

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

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

assert_eq!(TWO, &[3, 5]);
assert_eq!(FOUR, &[3, 5, 8, 13]);
assert_eq!(NONE, &[]);
assert_eq!(ALL, FIBB);