Skip to content

Closure tries to borrow &mut to entire struct, when only a field is required #19004

Description

@mihneadb

Offending code:

struct Foo {
    b: i32,
    v: Vec<i32>
}

fn incr(x: &mut i32) {
    *x += 1;
}

fn main() {
    let mut f = Foo { b: 0, v: vec![1, 2, 3] };

    f.v.iter().map(|s| {
        incr(&mut f.b);
        println!("{}: {}", f.b, s);
    }).last();
}

Error: error: cannot borrow fas mutable becausef.v is also borrowed as immutable

On playpen: http://is.gd/PTXhJq

Version info:

rustc 0.13.0-nightly (d91a015ab 2014-11-14 23:37:27 +0000)
binary: rustc
commit-hash: d91a015ab4c538f02fbbc03dfb08193a381c2e3b
commit-date: 2014-11-14 23:37:27 +0000
host: x86_64-unknown-linux-gnu
release: 0.13.0-nightly

Thanks @bjz for helping me submit this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-closuresArea: Closures (`|…| { … }`)C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions