rust - Conditional compilation with a if let enum matching which consists of one item -
i have following enum
:
pub enum game { match(gameworker), #[cfg(feature = "cups")] cup(cupworker), }
so, enum
consists of 1 item if cups
feature disabled. code below match
compiles okay in place use if let
s on matching enum
there error:
working match
:
fn clear(&mut self, silent: bool) { match *self { game::match(ref mut gm) => gm.clear(silent), #[cfg(feature = "cups")] game::cup(ref mut c) => c.clear(silent), } }
if let
leads compile error:
let m: &mut game = game::match(...); if let game::match(ref mut gamematch) = *m { // ... }
error:
error[e0162]: irrefutable if-let pattern --> src/game.rs:436:32 | 436 | if let game::match(ref mut gamematch) = *m { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ irrefutable pattern
is there way allow such if let
s ? construction somewhy not allowed use it, don't understand why. shown above, match
construction works okay in same case. in personal opinion here should silenceable warning
instead of error
.
if let
expects refutable pattern, similar how if
expects bool
. can't write if () { }
, though ()
"valid" in sense. if had if () {} else { something_else }
statically known else cannot occur.
arguably if true { }
statically known, there's difference: condition bool
, has 2 values, if statically know value, type still offers multiple variants.
with if let
it's same, can use user defined types instead of bool
. if enum has multiple variants, can't statically decide if let taken. if enum has single variant, know fact if condition true, if had else
branch, not make sense @ exist.
Comments
Post a Comment