Closed
Description
I tried this code:
fn let_chains(entry: std::io::Result<std::fs::DirEntry>) {
if let Ok(entry) = entry
&& let Some(s) = entry.file_name().to_str()
&& s.contains("")
{
println!("will fail to compile");
}
}
fn multiple_ifs(entry: std::io::Result<std::fs::DirEntry>) {
if let Ok(entry) = entry {
if let Some(s) = entry.file_name().to_str() {
if s.contains("") {
println!("will compile successfully");
}
}
}
}
I expected to see this happen: both compiles successfully
Instead, this happened: let_chains
fails to compile
error[E0716]: temporary value dropped while borrowed
--> src/lib.rs:3:26
|
3 | && let Some(s) = entry.file_name().to_str()
| ^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
| |
| creates a temporary which is freed while still in use
4 | && s.contains("")
| -------------- borrow later used here
|
= note: consider using a `let` binding to create a longer lived value
For more information about this error, try `rustc --explain E0716`.
error: could not compile `rust2` due to previous error
Meta
rustc --version --verbose
:
rustc 1.65.0-nightly (d394408fb 2022-08-07)
binary: rustc
commit-hash: d394408fb38c4de61f765a3ed5189d2731a1da91
commit-date: 2022-08-07
host: x86_64-unknown-linux-gnu
release: 1.65.0-nightly
LLVM version: 14.0.6