Skip to content

Commit 306c2c6

Browse files
committed
Mention 'move' and 'async' closure keywords
1 parent 08e372d commit 306c2c6

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

text/0000-closure-lifetime-binder.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ Unfortunately, Rust does not currently allow the signature of such a closure to
179179

180180
# Reference-level explanation
181181

182-
We now allow closures to be written as `for<'a .. 'z>`, where `'a .. 'z` is a comma-separated sequence of zero or more lifetimes. The syntax is parsed identically to the `for<'a .. 'z>` in the function pointer type `for<'a .. 'z> fn(&'a u8, &'b u8) -> &'a u8`
182+
We now allow closures to be written with a `for<'a .. 'z>` prefix, where `'a .. 'z` is a comma-separated sequence of zero or more lifetimes. The syntax is parsed identically to the `for<'a .. 'z>` in the function pointer type `for<'a .. 'z> fn(&'a u8, &'b u8) -> &'a u8`.
183+
This can be use with or without the `move` keyword:
184+
185+
`for<'a .. 'z> |arg1, arg2, ..., argN| { ... }`
186+
`for<'a .. 'z> move |arg1, arg2, ..., argN| { ... }`
183187

184188
When this syntax is used, any lifetimes specified with the `for<>` binder are always treated as higher-ranked, regardless of any other hints we discover during type inference. That is, a closure of the form `for<'a, 'b> |first: &'a u8, second: &'b bool| -> &'b bool` will have a compiler-generated impl of the form:
185189

@@ -198,6 +202,15 @@ for<> || {}; // Compiler error: return type not specified
198202

199203
This restriction allows us to avoid specifying how elided lifetime should be treated inside a closure with an explicit `for<>`. We may decide to lift this restriction in the future.
200204

205+
Additionally, this syntax is currently incompatible with async closures:
206+
207+
```rust
208+
for<'a> async |arg: &'a u8| -> () {}; // Compare error: `for<>` syntax cannot be used with async closures
209+
for<'a> async move |arg: &'a u8| -> () {}; // Compare error: `for<>` syntax cannot be used with async closures
210+
```
211+
212+
This restriction may be lifted in the future, but the interactions between this feature and the `async` desugaring will need to be considered.
213+
201214
# Drawbacks
202215

203216
This slightly increases the complexity of the language and the compiler implementation. However, the syntax introduced (`for<'a>`) can already be used in both trait bounds and function pointer types, so we are not introducing any new concepts in the languages.

0 commit comments

Comments
 (0)