Macro enum_extract::extract
[−]
[src]
macro_rules! extract { (@ANON_TUPLE [$(,)*], [$($ids:ident)*], $($p:ident)::+, $t:expr) => { ... }; (@ANON_TUPLE [_], [$($ids:ident)*], $($p:ident)::+, $t:expr) => { ... }; (@ANON_TUPLE [_, $($more:tt)*], [$($ids:ident)*], $($p:ident)::+, $t:expr) => { ... }; ($($p:ident)::+ { $($i:ident),* $(,)* } , $t:expr) => { ... }; ($($p:ident)::+ ( $($its:tt)* ) , $t:expr) => { ... }; }
Extract the fields of a single variant from an enum, returning an
Option<T>
where T
is either the single field, or a tuple of each of the
fields in the order they are written.
Examples
Given the following enum:
enum Foo { A(i32), B(i32, i32), C { x: i32, y: i32 }, D { z: i32 }, }
If the variant matches, it produces a Some
of the fields in the matched
variant.
let a = Foo::A(10); assert_eq!(extract!(Foo::A(_), a), Some(10)); let d = Foo::D{ z: 20 }; assert_eq!(extract!(Foo::D{z}, d), Some(20));
If there is more than one field in the enum variant, it produces a Some
of a tuple
let b = Foo::B(10, 20); assert_eq!(extract!(Foo::B(_, _), b), Some((10, 20))); let c = Foo::C{ x: 30, y: 40 }; assert_eq!(extract!(Foo::C{x, y}, c), Some((30, 40))); // You can also control the order of the fields in the struct variant case! assert_eq!(extract!(Foo::C{y, x}, c), Some((40, 30)));
If the pattern doesn't match, it produces a None
let b = Foo::B(10, 20); assert_eq!(extract!(Foo::A(_), b), None);