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

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

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

Constness

This function takes constant time, and in order to be const fn it requires the “rust_1_64” feature to be enabled.

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