如何在Rust中不使用match比较两个enum值?
你好!我开始学习Rust,在处理enum时遇到了问题。我有一个表示订单状态的枚举:
```rust
#[derive(Debug)]
enum OrderStatus {
Pending,
Processing,
Shipped,
Delivered,
Cancelled,
}
```
我只需要检查当前状态是否等于某个特定变体,例如`Pending`。在其他语言(C++、Java)中,我可以写`if (status == OrderStatus::Pending)`,但在Rust中这不起作用:
```rust
let status = OrderStatus::Pending;
if status == OrderStatus::Pending {
println!("订单等待处理");
}
```
编译器报错:`binary operation `==` cannot be applied to type `OrderStatus``。我知道可以使用`match`,但对于这种简单的检查来说,它似乎过于繁琐:
```rust
match status {
OrderStatus::Pending => println!("订单等待处理"),
_ => {}
}
```
有没有办法像其他语言那样直接比较enum?也许可