简体   繁体   中英

How do I pass an enum variant to match on as a function parameter?

I would like to pass in the parameters what arm of the enum I need to match, something like this:

enum D {
    A(i64),
    B(u64),
    C(u64, u64),
}
let a = D.A(10);
println!(a.is_of(D.A)); // true
println!(a.is_of(D.B)); // false

I know I can use matching rules for this, but I'd like this is_of method to take as an input of the enum options for my purposes.

You cannot.

  • It is not possible to pass types as function parameters.
  • Enum variants are not types to start with.

If you are OK using a macro instead of a function, see

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM