Function array_bytes::prefix_with

source ·
pub fn prefix_with<A, T, const N: usize>(any: A, element: T) -> [T; N]
where A: AsRef<[T]>, T: Copy,
Expand description

Prefixes the given element to the given array/slice/vector to make it a fixed-size array of length N.

If the length of the array/slice/vector is already equal to N, it returns the array/slice/vector as a fixed-size array. If the length of the array/slice/vector is greater than N, it returns the first N elements of the array/slice/vector as a fixed-size array. If the length of the array/slice/vector is less than N, it creates a new fixed-size array of length N and copies the array/slice/vector into it, padding the remaining elements with the given element.

Examples

assert_eq!(array_bytes::prefix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
assert_eq!(array_bytes::prefix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
assert_eq!(array_bytes::prefix_with::<_, _, 5>([1, 2, 3], 0), [0, 0, 1, 2, 3]);