Skip to content

Parameter borrows should happen before receiver borrows #17365

Closed
@jethrogb

Description

@jethrogb

This works:

fn main() {
    let mut nums=vec![10i,11,12,13];
    let index=nums.len()-2;
    *nums.get_mut(index)=2;
}

I would like to be able to write this instead:

fn main() {
    let mut nums=vec![10i,11,12,13];
    *nums.get_mut(nums.len()-2)=2;
}

But:

test.rs:3:16: 3:20 error: cannot borrow `nums` as immutable because it is also borrowed as mutable
test.rs:3   *nums.get_mut(nums.len()-2)=2;
                          ^~~~
test.rs:3:3: 3:7 note: previous borrow of `nums` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `nums` until the borrow ends
test.rs:3   *nums.get_mut(nums.len()-2)=2;
             ^~~~

Lexically, the receivers appears before the parameters, but semantically it appears later, which is when I'd expect the borrow to happen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions