Support returning elements from an array by-value

71b0085
Opened by Ximin Luo at 2023-03-07 05:47:33

This works:

fn test_vec() -> String {
    let mut z = vec![String::from("foo"), String::from("bar")];
    z.remove(0)
}

But this fails:

fn test_array() -> String {
    let mut z = [String::from("foo"), String::from("bar")];
    z[0]
}

with a "cannot move out of" error.

(#rust-beginners advised me that this "should" intuitively work and that I should file here)

  1. Thanks infinity0; my thinking is that it would be nice to avoid the mem::swap() or mem::replace() dance to get one item out of an array if the array as a whole is going out of scope in the same statement.

    Thanks

    Seth Arnold at 2018-02-13 00:20:00

  2. This is https://github.com/rust-lang/rfcs/issues/997.

    Steven Fackler at 2018-02-13 00:26:05

  3. Triage: no change

    Maayan Hanin at 2022-03-22 07:28:31