作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个枚举:
pub enum Enum1 {
A(String),
B(i64),
C(f64)
}
我如何对 A 进行模式匹配?也就是说,我想得到它的String值。我试过这个:
match optionMyEnum {
Some(A(x: String)) => ...
并得到了大量的编译错误。
最佳答案
The Rust Programming Language有一个完整的 section on matching .我强烈鼓励您阅读该部分(以及整本书)。该文档投入了大量时间和精力。
您只需指定一个要绑定(bind)的名称。无需写出类型:
pub enum Enum {
A(String),
B(i64),
C(f64),
}
fn main() {
let val = Enum::A("hello".to_string());
match val {
Enum::A(x) => println!("{}", x),
_ => println!("other"),
}
}
在许多情况下,您会希望绑定(bind)到值的引用:
Enum::A(ref x) => println!("{}", x),
关于rust - 与枚举的构造函数匹配的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984644/
我是一名优秀的程序员,十分优秀!