1
Fork 0

Rollup merge of #81634 - jesusprubio:jesusprubio/add-long-explanation-e0521, r=GuillaumeGomez

Add long explanation e0521

Helps with #61137
This commit is contained in:
Jack Huey 2021-02-02 16:01:44 -05:00 committed by GitHub
commit fd4f4adede
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 55 additions and 4 deletions

View file

@ -267,6 +267,7 @@ E0516: include_str!("./error_codes/E0516.md"),
E0517: include_str!("./error_codes/E0517.md"),
E0518: include_str!("./error_codes/E0518.md"),
E0520: include_str!("./error_codes/E0520.md"),
E0521: include_str!("./error_codes/E0521.md"),
E0522: include_str!("./error_codes/E0522.md"),
E0524: include_str!("./error_codes/E0524.md"),
E0525: include_str!("./error_codes/E0525.md"),
@ -597,7 +598,6 @@ E0780: include_str!("./error_codes/E0780.md"),
E0514, // metadata version mismatch
E0519, // local crate and dependency have same (crate-name, disambiguator)
// two dependencies have same (crate-name, disambiguator) but different SVH
E0521, // borrowed data escapes outside of closure
E0523,
// E0526, // shuffle indices are not constant
// E0540, // multiple rustc_deprecated attributes

View file

@ -0,0 +1,28 @@
Borrowed data escapes outside of closure.
Erroneous code example:
```compile_fail,E0521
let mut list: Vec<&str> = Vec::new();
let _add = |el: &str| {
list.push(el); // error: `el` escapes the closure body here
};
```
A type anotation of a closure parameter implies a new lifetime declaration.
Consider to drop it, the compiler is reliably able to infer them.
```
let mut list: Vec<&str> = Vec::new();
let _add = |el| {
list.push(el);
};
```
See the [Closure type inference and annotation][closure-infere-annotation] and
[Lifetime elision][lifetime-elision] sections of the Book for more details.
[closure-infere-annotation]: https://doc.rust-lang.org/book/ch13-01-closures.html#closure-type-inference-and-annotation
[lifetime-elision]: https://doc.rust-lang.org/reference/lifetime-elision.html