konst::slice

Function slice_up_to

Source
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.

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