Is it possible to prepend a value to an array? I know how to concatenate two arrays, but if I have an array and a single value (of the same type as the array), can I prepend this element to the array?
There is no way to do this in stable Rust; arrays cannot have values added or removed at runtime; their lengths are fixed at compile time.
It’s more likely you want a Vec and Vec::insert.
See also:
In nightly Rust, you can use unstable features to construct a brand new array that's slightly bigger and move all the values over:
// 1.52.0-nightly (2021-03-07 234781afe33d3f339b00)
#![allow(incomplete_features)]
#![feature(const_generics, const_evaluatable_checked)]
use std::{
array::IntoIter,
mem::{self, MaybeUninit},
ptr,
};
fn prepend<T, const N: usize>(a: [T; N], v: T) -> [T; N + 1] {
// # SAFETY
//
// Converting an uninitialized array to an array of
// uninitialized values is always safe.
// https://github.com/rust-lang/rust/issues/80908
let mut xs: [MaybeUninit<T>; N + 1] = unsafe { MaybeUninit::uninit().assume_init() };
let (head, tail) = xs.split_first_mut().unwrap();
*head = MaybeUninit::new(v);
for (x, v) in tail.iter_mut().zip(IntoIter::new(a)) {
*x = MaybeUninit::new(v)
}
// # SAFETY
//
// We are effectively transmuting from an array of filled `MaybeUninit<T>` to
// the array of `T`, but cannot actually use `transmute`:
// https://github.com/rust-lang/rust/issues/61956
unsafe {
let tmp_xs = &mut xs as *mut [MaybeUninit<T>; N + 1] as *mut [T; N + 1];
mem::forget(xs);
ptr::read(tmp_xs)
}
}
fn main() {
let v = prepend([1, 2, 3], 4);
assert_eq!([4, 1, 2, 3], v);
}
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With