Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prepend a value in an array?

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?

like image 522
Ziva Avatar asked May 02 '26 20:05

Ziva


1 Answers

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:

  • How to set a Rust array length dynamically?
  • What is the difference between a slice and an array?
  • What is the difference between storing a Vec vs a Slice?
  • Efficiently insert or replace multiple elements in the middle or at the beginning of a Vec?

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:

  • Is it possible to control the size of an array using the type parameter of a generic?
  • How do I move values out of an array one at a time?
  • How do I collect into an array?
like image 77
Shepmaster Avatar answered May 04 '26 14:05

Shepmaster