pub const fn slice_up_to_len_alt<T>(slice: &[T], len: usize) -> &[T]
Available on crate feature fmt only.
Expand description

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

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

Runtime

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

If the “rust_1_64” feature is enabled, it takes constant time to run.

Example

use const_format::utils::slice_up_to_len_alt;

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

const TWO: &[u16] = slice_up_to_len_alt(FIBB, 2);
const FOUR: &[u16] = slice_up_to_len_alt(FIBB, 4);
const ALL: &[u16] = slice_up_to_len_alt(FIBB, usize::MAX);

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