Closed
Description
$ cargo clippy -V
0.0.212
The following code:
fn read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
let line = stdin.lock().lines().next().unwrap().unwrap();
line
}
Triggers the let_and_return lint, however upon applying the suggested fix as so:
fn read_line() -> String {
use std::io::BufRead;
let stdin = ::std::io::stdin();
stdin.lock().lines().next().unwrap().unwrap()
}
The code fails to compile with the following message:
error[E0597]: `stdin` does not live long enough
--> src\main.rs:8:5
|
8 | stdin.lock().lines().next().unwrap().unwrap()
| ^^^^^---------------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
9 | }
| -
| |
| `stdin` dropped here while still borrowed
| ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `std::io::Lines<std::io::StdinLock<'_>>`
|
= note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could
save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block
Basically the compiler suggest I undo the change I just made.