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

IndexMut #72824

Closed
ghost opened this issue May 31, 2020 · 3 comments
Closed

IndexMut #72824

ghost opened this issue May 31, 2020 · 3 comments
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@ghost
Copy link

ghost commented May 31, 2020

This gives an error:

fn main() {
    let mut vec = vec![1; 3];
    vec[vec.len() - 1] = 2;
}
error[E0502]: cannot borrow `vec` as immutable because it is also borrowed as mutable
   |
   |     vec[vec.len() - 1] = 2;
   |     ----^^^-----------
   |     |   |
   |     |   immutable borrow occurs here
   |     mutable borrow occurs here
   |     mutable borrow later used here

But this gives no error:

use std::ops::IndexMut;
fn main() {
    let mut vec = vec![1; 3];
    *vec.index_mut(vec.len() - 1) = 2;
}
@Elinvynia Elinvynia added A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels May 31, 2020
@Elinvynia
Copy link
Contributor

Related docs: https://doc.rust-lang.org/std/ops/trait.IndexMut.html

Assuming the docs are not the issue here, this should work in my opinion.

@RustyYato
Copy link
Contributor

This is because two-phased borrows only work with method syntax, maybe we should also allow it for index sugar?

@jonas-schievink
Copy link
Contributor

Duplicate of #58419

@jonas-schievink jonas-schievink marked this as a duplicate of #58419 Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants