Skip to content

Commit 0cf7d17

Browse files
committed
// enums2.rs
// // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a // hint. // I AM NOT DONE #[derive(Debug)] enum Message { // 变体携带一个包含x和y字段的结构体 Move { x: i32, y: i32 }, // 变体携带一个String类型的值 Echo(String), // 变体携带三个i32类型的值(分别表示RGB颜色分量) ChangeColor(i32, i32, i32), // 单元变体,不携带任何数据 Quit, } impl Message { fn call(&self) { println!("{:?}", self); } } fn main() { let messages = [ Message::Move { x: 10, y: 30 }, Message::Echo(String::from("hello world")), Message::ChangeColor(200, 255, 255), Message::Quit, ]; for message in &messages { message.call(); } }
1 parent 05fb2be commit 0cf7d17

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

exercises/enums/enums2.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77

88
#[derive(Debug)]
99
enum Message {
10-
// TODO: define the different variants used below
10+
// 变体携带一个包含x和y字段的结构体
11+
Move { x: i32, y: i32 },
12+
// 变体携带一个String类型的值
13+
Echo(String),
14+
// 变体携带三个i32类型的值(分别表示RGB颜色分量)
15+
ChangeColor(i32, i32, i32),
16+
// 单元变体,不携带任何数据
17+
Quit,
1118
}
1219

1320
impl Message {
@@ -28,3 +35,4 @@ fn main() {
2835
message.call();
2936
}
3037
}
38+

0 commit comments

Comments
 (0)