Add long error explanation for E0521

This commit is contained in:
Jesus Rubio
2021-02-01 18:22:13 +01:00
parent e0d9f79399
commit ee5ea24d36
2 changed files with 29 additions and 1 deletions

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