在以下代码中,如何访问字符串"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();
由于enum Message
可以处于多种状态,因此您必须处于正确的状态才能提取问候。
fn call(&self) {
match self {
Message::Write(string) => println!("{}", string),
_ => {},
}
}
编辑:user4815162342解决方案也是正确的,您可以使用match
或if let
互换使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句