rust-quiz
rust-quiz copied to clipboard
Idea: `let _ = ` vs `_ = `
Discovered today: both let _ =
and _ =
(destructuring assignment) can be used to ignore #[must_use]
, however because the former is a statement while the latter is an expression they have a difference with temporary lifetime extension:
struct NoisyDrop;
impl Drop for NoisyDrop {
fn drop(&mut self) {
println!("NoisyDrop drop");
}
}
fn main() {
let _ = &NoisyDrop;
println!("after");
}
after
NoisyDrop drop
While:
struct NoisyDrop;
impl Drop for NoisyDrop {
fn drop(&mut self) {
println!("NoisyDrop drop");
}
}
fn main() {
_ = &NoisyDrop;
println!("after");
}
NoisyDrop drop
after