Function konst::slice::try_into_array_mut[][src]

pub const fn try_into_array_mut<T, const N: usize>(
    slice: &mut [T]
) -> Result<&mut [T; N], TryIntoArrayError>
This is supported on crate features mut_refs or nightly_mut_refs only.
Expand description

Tries to convert from &mut [T] to &mut [T; N].

Returns an Err(TryIntoArrayError{..}) when the slice doesn’t match the expected length.

Example

use konst::{slice, unwrap_ctx};

const fn mut_array_from<const LEN: usize>(slice: &mut [u8], from: usize) -> &mut [u8; LEN] {
    let sliced = slice::slice_range_mut(slice, from, from + LEN);
    unwrap_ctx!(slice::try_into_array_mut(sliced))
}


let slice = &mut [3, 5, 8, 13, 21, 34, 55, 89, 144, 233];

let foo: &mut [u8; 2] = mut_array_from(slice, 0);
assert_eq!(foo, &mut [3, 5]);

let bar: &mut [u8; 3] = mut_array_from(slice, 2);
assert_eq!(bar, &mut [8, 13, 21]);

let baz: &mut [u8; 4] = mut_array_from(slice, 4);
assert_eq!(baz, &mut [21, 34, 55, 89]);