pub const fn split_first<T>(slice: &[T]) -> Option<(&T, &[T])>
Expand description
A const equivalent of
<[T]>::split_first
ยงExample
use konst::slice;
const fn add_up(mut slice: &[u32]) -> u64 {
let mut ret = 0u64;
while let Some((first, rem)) = slice::split_first(slice) {
ret += *first as u64;
// advances the slice
slice = rem;
}
ret
}
assert_eq!(add_up(&[1]), 1);
assert_eq!(add_up(&[1, 2]), 3);
assert_eq!(add_up(&[1, 2, 3]), 6);
assert_eq!(add_up(&[1, 2, 3, 4]), 10);