W3cubDocs

/Rust

Error code E0521

Borrowed data escapes outside of closure.

Erroneous code example:

#![allow(unused)]
fn main() {
let mut list: Vec<&str> = Vec::new();

let _add = |el: &str| {
    list.push(el); // error: `el` escapes the closure body here
};
}

A type annotation of a closure parameter implies a new lifetime declaration. Consider to drop it, the compiler is reliably able to infer them.

#![allow(unused)]
fn main() {
let mut list: Vec<&str> = Vec::new();

let _add = |el| {
    list.push(el);
};
}

See the Closure type inference and annotation and Lifetime elision sections of the Book for more details.

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/error_codes/E0521.html