简体   繁体   English

如何传递枚举变量以匹配作为函数参数?

[英]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. 我知道我可以为此使用匹配规则,但是出于我的目的,我希望将此is_of方法用作枚举选项的输入。

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: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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