]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0521.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0521.md
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 annotation 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