You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test.rs:9:15: 9:18 error: `str` does not live long enough
test.rs:9 error(str.as_slice());
^~~
test.rs:1:15: 11:2 note: reference must be valid for the block at 1:14...
test.rs:1 pub fn main() {
test.rs:2 let error = | err | {
test.rs:3 println!("error: {}", err);
test.rs:4 };
test.rs:5
test.rs:6 {
...
test.rs:6:5: 10:6 note: ...but borrowed value is only valid for the block at 6:4
test.rs:6 {
test.rs:7 let str = "foo".to_string();
test.rs:8
test.rs:9 error(str.as_slice());
test.rs:10 }
error: aborting due to previous error
The code builds if I annotate the closure with | err: &str|
The text was updated successfully, but these errors were encountered:
This is intended. Closures capture references to their environment, and your str goes out of scope at the end of the block, just like the error says. This would be a dangling pointer otherwise.
The following code:
Produces the following error:
The code builds if I annotate the closure with
| err: &str|
The text was updated successfully, but these errors were encountered: