While trying to print out my home directory I came across the following error:
fn main() {
let home = home::home_dir().unwrap().to_str().unwrap();
println!("home dir: {}", home);
}
error[E0716]: temporary value dropped while borrowed
--> src/main.rs:3:16
|
3 | let home = home::home_dir().unwrap().to_str().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
4 | println!("home dir: {}", home);
| ---- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
But if I split the part underlined in the error message and put it in a separate variable it works:
fn main() {
let home_buf = home::home_dir().unwrap();
let home_str = home_buf.to_str().unwrap();
println!("home dir: {}", home_str);
}
My question is what's the difference between the two examples?
(I'm using the home crate in my code)