What does this code output?
#[()]
pub enum {
,
}
fn () {
let = ::;
if !(::, ) {
!("{fruit:?} is Apple");
} else {
!("{fruit:?} is Orange");
}
}
Free fruit at the fruit store if you guessed Orange is Apple
!
During development, I frequently ignore common compiler warnings like unused variables
because they are usually leftovers from past versions of code that I clean up later.
It bit me today because I accidentally inverted the order of parameters in the matches!
macro, which matched Fruit::Apple
to the catch-all variable fruit
(unrelated to the local variable declared on line 10).
This can be seen with cargo-expand:
#![(prelude_import)]
#[]
use std::prelude::::*;
#[]
extern crate std;
pub enum {
,
,
}
#[]
impl ::core::fmt:: for {
#[]
fn (&, : &mut ::core::fmt::) -> ::core::fmt:: {
::core::fmt::::(
,
match {
:: => "Apple",
:: => "Orange",
},
)
}
}
fn () {
let = ::;
if match :: {
=> true,
_ => false,
} {
{
::std::io::(!("{0:?} is Apple\n", ));
};
} else {
{
::std::io::(!("{0:?} is Orange\n", ));
};
}
}
To be fair, the compiler does raise a warning, but it’s vague and as previously mentioned, easily glossed over:
warning: unused variable: `fruit`
--> src/main.rs:11:31
|
11 | if matches!(Fruit::Apple, fruit) {
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_fruit`
|
= note: `#[warn(unused_variables)]` on by default
warning: `testing` (bin "testing") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/testing`
Orange is Apple