Skip to content

Procs don't allow capturing mutable variables #10617

@alexcrichton

Description

@alexcrichton

This code fails to compile

fn foo(_: proc()) {}

fn bar(_: &mut int) {}

fn main() {
    let mut a = 3;
    do foo {
        bar(&mut a);
    }
}

with

test.rs:8:12: 8:18 error: cannot borrow immutable captured outer variable in a heap closure as mutable
test.rs:8         bar(&mut a);
                      ^~~~~~
test.rs:8:17: 8:18 error: mutable variables cannot be implicitly captured
test.rs:8         bar(&mut a);
                           ^
error: aborting due to 2 previous errors
task 'rustc' failed at 'explicit failure', /Users/alex/code/rust-opt/src/libsyntax/diagnostic.rs:101
task '<main>' failed at 'explicit failure', /Users/alex/code/rust-opt/src/librustc/lib.rs:396

Since procs are one-shot, I don't see why I can't capture the value mutably, the proc gains ownership of the type.

cc @pcwalton, @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions