You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-closure-lifetime-binder.md
+14-1
Original file line number
Diff line number
Diff line change
@@ -179,7 +179,11 @@ Unfortunately, Rust does not currently allow the signature of such a closure to
179
179
180
180
# Reference-level explanation
181
181
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:
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:
185
189
@@ -198,6 +202,15 @@ for<> || {}; // Compiler error: return type not specified
198
202
199
203
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.
200
204
205
+
Additionally, this syntax is currently incompatible with async closures:
206
+
207
+
```rust
208
+
for<'a> async|arg:&'au8|-> () {}; // Compare error: `for<>` syntax cannot be used with async closures
209
+
for<'a> asyncmove|arg:&'au8|-> () {}; // 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
+
201
214
# Drawbacks
202
215
203
216
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