This code shows an error:
fn main() {
let mut writer = std::io::BufWriter::new(std::io::stdout().lock());
}
error[E0716]: temporary value dropped while borrowed
--> src\main.rs:5:46
|
5 | let mut writer = std::io::BufWriter::new(std::io::stdout().lock());
| ^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
...
12 | }
| - borrow might be used here, when `writer` is dropped and runs the `Drop` code for type `BufWriter`
|
= note: consider using a `let` binding to create a longer lived value
This code works:
fn main() {
let stdout = std::io::stdout();
let mut writer = std::io::BufWriter::new(stdout.lock());
}
How is the borrow checker working here?