pub fn prefix_with<A, T, const N: usize>(any: A, element: T) -> [T; N]
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>([5, 2, 0, 1], 0), [5, 2, 0, 1]);
assert_eq!(array_bytes::prefix_with::<_, _, 4>([5, 2, 0, 1, 3, 1, 4], 0), [5, 2, 0, 1]);
assert_eq!(array_bytes::prefix_with::<_, _, 5>([5, 2, 0], 0), [0, 0, 5, 2, 0]);