Skip to content

by-ref closure capture semantics are a backwards compatibility hazard #12831

Closed
@thestinger

Description

@thestinger

Unboxed closures (#8622) would capture by-value, and there would be no need to add syntax for a by-reference capture. Rust's references are first-class values, unlike C++, so it's trivial to capture a reference by-value.

This would create an unboxed closure (f) with the same size as b:

let b = 5;
let f = |a| a + b;

A by-reference capture would need to be explicit:

let b = &mut 5;
let f = |a| *b = a;

I think we should consider changing the current rules, to permit dropping in unboxed closures as a backwards compatible enhancement in the future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions