Skip to content

|&mut:| wants (Copy) upvar to be mutable when it shouldn't #16749

Closed
@japaric

Description

@japaric

STR

#![feature(overloaded_calls, unboxed_closures)]

use std::mem;

fn main() {
    // error: cannot assign to immutable captured outer variable in a proc
    //let n = 0u8;
    let mut n = 0u8;

    let mut f = |&mut:| {
        n += 1;
        println!("f.n = {}", n);
    };

    println!("Closure size: {}", mem::size_of_val(&f));
    f();
    f();
    f();
    println!("n = {}", n);
}

Output

Closure size: 1
f.n = 1
f.n = 2
f.n = 3
n = 0

Correct me if I'm wrong, but I think n shouldn't need to be mutable, because f captures (by value) a copy of n, and modifies its copy of n on each call. Whereas, the outer n remains unchanged.

cc @pcwalton

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions