Skip to content

Commit ee5ea24

Browse files
committed
Add long error explanation for E0521
1 parent e0d9f79 commit ee5ea24

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

compiler/rustc_error_codes/src/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ E0516: include_str!("./error_codes/E0516.md"),
267267
E0517: include_str!("./error_codes/E0517.md"),
268268
E0518: include_str!("./error_codes/E0518.md"),
269269
E0520: include_str!("./error_codes/E0520.md"),
270+
E0521: include_str!("./error_codes/E0521.md"),
270271
E0522: include_str!("./error_codes/E0522.md"),
271272
E0524: include_str!("./error_codes/E0524.md"),
272273
E0525: include_str!("./error_codes/E0525.md"),
@@ -597,7 +598,6 @@ E0780: include_str!("./error_codes/E0780.md"),
597598
E0514, // metadata version mismatch
598599
E0519, // local crate and dependency have same (crate-name, disambiguator)
599600
// two dependencies have same (crate-name, disambiguator) but different SVH
600-
E0521, // borrowed data escapes outside of closure
601601
E0523,
602602
// E0526, // shuffle indices are not constant
603603
// E0540, // multiple rustc_deprecated attributes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Borrowed data escapes outside of closure.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0521
6+
let mut list: Vec<&str> = Vec::new();
7+
8+
let _add = |el: &str| {
9+
list.push(el); // error: `el` escapes the closure body here
10+
};
11+
```
12+
13+
A type anotation of a closure parameter implies a new lifetime declaration.
14+
Consider to drop it, the compiler is reliably able to infer them.
15+
16+
```
17+
let mut list: Vec<&str> = Vec::new();
18+
19+
let _add = |el| {
20+
list.push(el);
21+
};
22+
```
23+
24+
See the [Closure type inference and annotation][closure-infere-annotation] and
25+
[Lifetime elision][lifetime-elision] sections of the Book for more details.
26+
27+
[closure-infere-annotation]: https://doc.rust-lang.org/book/ch13-01-closures.html#closure-type-inference-and-annotation
28+
[lifetime-elision]: https://doc.rust-lang.org/reference/lifetime-elision.html

0 commit comments

Comments
 (0)