Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Propose more relaxtions to closure catpures #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lang-changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Language Changes

This document lists changes that will be made to the language along with RFC-2229.

For this document let RS20 be the pre-RFC compiler and RS21 be the post-RFC compiler.

## `_` pattern in closures

Consider the following code

```rust
let x;
let c = || {
let _ = x;
};
```

RS20 will result in the capture of x within the closure, but `_` is just an ignore/discard and that means x can't/won't be used within the closure and should not result in a capture.

For RS21 we propose to ignore such captures allowing the following pieces of code to be accepted by RS21

```rust
let x;
let c = || {
let _ = x;
};

let f : fn() = c;

f();
```

The above code works now because `x` isn't captured by `c` anymore, making `c` a non-capturing closure, which can be coerced to a `FnPtr`.

```rust
fn main() {
let mut x = String::from("1");

let c = || {
let _ = x;
};


let mx = &mut x;
*mx = String::from("2");

c();

println!("{}", x);
}
```
RS20 would immutably borrow x within the closure (even thought it's discarded), preventing a mutable borrow to exist outside the closure.
The above code works with RS21 because `x` isn't captured within the closure allowing the mutable borrow outside the closure to exist.