How do I concatenate two slices in Rust?

Just use .concat() on a slice of slices:

fn main() {
    let v = (0u64 .. 10).collect::<Vec<_>>();
    let l = v.len();
    let first_and_last = [&v[..3], &v[l - 3..]].concat();
    println!("{:?}", first_and_last);
    // The output is `[0, 1, 2, 7, 8, 9]`
}

This creates a new vector, and it works with arbitrary number of slices.

(Playground link)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)