Skip to content

Instantly share code, notes, and snippets.

@tioover
Last active August 29, 2015 14:13
Show Gist options
  • Save tioover/6442b5e520c439d3d81c to your computer and use it in GitHub Desktop.
Save tioover/6442b5e520c439d3d81c to your computer and use it in GitHub Desktop.
enum Foobar { // 创建一个枚举类型
Foo (i32, i32),
Bar (i32, i32),
}
let foobar = Foobar::Foo(42, 233);
match foobar {
Foobar::Foo (x, y) => // 匹配 Foo 枚举项。
println!("x is {}, y is {}", x, y), // 将匹配到的元素输出。
Foobar::Bar (n) => // 匹配 Bar 枚举项
println!( // 输出下面 match 表达式的返回值。
match n { // 再次匹配匹配到的元素。
42 => "The Answer to the Ultimate Question of Life, the Universe, and Everything", // 终极答案。
2|3|5|7 => "prime < 10", // 十以内的素数。
x if x % 2 == 0 => "even", // 偶数。
_ => "odd", // 奇数。
}
),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment