Skip to content

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

Closed

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.

Activity

  1. pnkfelix commented on Mar 13, 2014

    @pnkfelix
    Contributor

    We do need to address the problem described here; it is not yet clear whether the suggested strategy is what we will follow, but we need to do something.

    Assigning 1.0, P-backcompat-lang.

  2. added this to the 1.0 milestone on Mar 13, 2014
  3. added a commit that references this issue on May 29, 2014
    a43ff2d
  4. added a commit that references this issue on Aug 14, 2014
  5. nikomatsakis commented on Jan 8, 2015

    @nikomatsakis
    Contributor

    This is done.

  6. added a commit that references this issue on Jul 22, 2022
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions