rust - Matching with less boilerplate -
i have nested structures of enums
, ie. enum x
has variant contains enum y
etc. there way access fields eg x.y.z
runtime error if x.y.z
doesn't exist (eg, x
of wrong variant). furthermore reasonably moral thing do, or there better alternative, without match
statements everywhere? efficiency not important, though make cheap possible.
it not possible access nested enums dots syntax, can use if let
:
pub enum x { a(y), b(z) } pub enum y { c, d(u32) } pub enum z { e } fn main() { let x = x::a(y::d(12)); if let x::a(y::d(n)) = x { println!("got it: {}", n); } else { println!("nope"); } }
(try here)
if let
makes code arguably more concise match
. naturally, efficient match
.
Comments
Post a Comment