Skip to content

Repeated "mutable/immutable borrow" error messages #42106

Closed
@tommyip

Description

@tommyip

Minimal example:

fn do_something<T>(collection: &mut Vec<T>) {
    let a = &collection;
    collection.swap(1, 2);
}

Error message (ran in playground):

rustc 1.17.0 (56124baa9 2017-04-24)
error[E0502]: cannot borrow `*collection` as mutable because `collection` is also borrowed as immutable
 --> <anon>:3:5
  |
2 |     let a = &collection;
  |              ---------- immutable borrow occurs here
3 |     collection.swap(1, 2);
  |     ^^^^^^^^^^ mutable borrow occurs here
4 | }
  | - immutable borrow ends here

error[E0502]: cannot borrow `*collection` as mutable because `collection` is also borrowed as immutable
 --> <anon>:3:5
  |
2 |     let a = &collection;
  |              ---------- immutable borrow occurs here
3 |     collection.swap(1, 2);
  |     ^^^^^^^^^^ mutable borrow occurs here
4 | }
  | - immutable borrow ends here

error: aborting due to 2 previous errors

Compilation failed.

Tested on nightly as well (rustc 1.19.0-nightly (0ed1ec9f9 2017-05-18))

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions