获得通配符臂的价值

时间:2014-10-19 15:56:24

标签: wildcard rust

如何在match语句中获取通配符臂的值?

例如:

let a = 1i;
let b = 2i;
match a.cmp(&b) {
    Greater => println!("is greater"),
    _ => println!("is {}", _) // error: unexpected token: `_`
}

我希望能比在变量中存储匹配的enum更清晰:

let a = 1i;
let b = 2i;
let ord = a.cmp(&b);
match ord {
    Greater => println!("is greater"),
    _ => println!("is {}", ord)
}

1 个答案:

答案 0 :(得分:1)

这是你要求的吗?

let a = 1i;
let b = 2i;
match a.cmp(&b) {
    Greater => println!("is greater"),
    e => println!("is {}", e)
}