如何传递枚举变量以匹配作为函数参数? [英] 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
我知道我可以为此使用匹配规则,但是出于我的目的,我希望将此is_of
方法用作枚举选项的输入.
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.
推荐答案
您不能.
- 无法将类型作为函数参数传递.
- 枚举变体不是开头的类型.
如果可以使用宏而不是函数,请参见
If you are OK using a macro instead of a function, see
- How do I assert an enum is a specific variant if I don't care about its fields?
- The matches crate
另请参阅:
这篇关于如何传递枚举变量以匹配作为函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文