【发布时间】:2020-01-01 11:32:05
【问题描述】:
在下面的代码中,如何访问字符串"hello"(我在定义m时传递的)?
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
impl Message {
fn call(&self) {
// How to access "hello" string from here?
}
}
let m = Message::Write(String::from("hello"));
m.call();
【问题讨论】:
-
与
if let Message::Write(x) = self { ... }的模式匹配