Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning elements from an array by-value #48174

Open
infinity0 opened this issue Feb 13, 2018 · 3 comments
Open

Support returning elements from an array by-value #48174

infinity0 opened this issue Feb 13, 2018 · 3 comments
Labels
A-array Area: `[T; N]` A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@infinity0
Copy link
Contributor

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)

@setharnold
Copy link

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

@sfackler
Copy link
Member

This is rust-lang/rfcs#997.

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 13, 2018
@Spoonbender
Copy link

Triage: no change

@workingjubilee workingjubilee added the A-array Area: `[T; N]` label Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-array Area: `[T; N]` A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants