一、
- 枚举例子:
enum IpAddrKind {
V4,
V6,
}
fn main() {
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
route(four);
route(six);
}
fn route(ip_kind: IpAddrKind) {}
- 将数据附加到枚举的变体中:
未附加:
enum IpAddrKind {
V4,
V6,
}
struct IpAddr {
kind: IpAddrKind,
address: String,
}
fn main() {
let home = IpAddr{
kind: IpAddrKind::V4,
address: String::from("127.0.0.1"),
};
let loopback = IpAddr {
kind: IpAddrKind::V6,
address: String::from("::1"),
};
}
已附加(不需要额外使用Struct):
enum IpAddrKind {
V4(u8, u8, u8, u8),
V6(String),
}
fn main() {
let home = IpAddrKind::V4(127, 0, 0, 1);
let loopback = IpAddrKind::V6(String::from("::1"));
}
enum Message {
Quit,
Move {
x: i32,
y: i32
},
Write(String),
ChangeColor(i32, i32, i32),
}
fn main() {
let q = Message::Quit;
let m = Message::Move { x: 12, y: 24 };
let w = Message::Write(String::from("Hello"));
let c = Message::ChangeColor(0, 255, 255);
}
- 为枚举定义方法(impl):
enum Message {
Quit,
Move {
x: i32,
y: i32
},
Write(String),
ChangeColor(i32, i32, i32),
}
impl Message {
fn call(&self) {}
}
fn main() {
let q = Message::Quit;
let m = Message::Move { x: 12, y: 24 };
let w = Message::Write(String::from("Hello"));
let c = Message::ChangeColor(0, 255, 255);
m.call();
}
二、枚举与模式匹配(Option)
- Rust没有Null,Rust中类似Null概念的枚举——Option<T>
fn main() {
let some_number = Some(5);
let some_string = Some("A String");
let absent_number: Option<i32> = None;
}
- Option<T>和T是不同的类型,不可以吧Option<T>直接当成T:
fn main() {
let x: i8 = 5;
let y: Option<i8> = Some(5);
let sum = x + y;
}
cannot add `Option<i8>` to `i8`
--> src\main.rs:5:17
|
5 | let sum = x + y;
| ^ no implementation for `i8 + Option<i8>`
|
= help: the trait `Add<Option<i8>>` is not implemented for `i8`
= help: the following other types implement trait `Add<Rhs>`:
<&'a f32 as Add<f32>>
<&'a f64 as Add<f64>>
<&'a i128 as Add<i128>>
<&'a i16 as Add<i16>>
<&'a i32 as Add<i32>>
<&'a i64 as Add<i64>>
<&'a i8 as Add<i8>>
<&'a isize as Add<isize>>
and 48 others
For more information about this error, try `rustc --explain E0277`.
error: could not compile `explorer` due to previous error
解决(Option<T>转化成T)
三、枚举与模式匹配
- 控制流运算符 -match
- 允许一个值与一系列模式进行匹配,并执行匹配模式对应的代码
- 例子:
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("yes");
1
},
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
fn main() {}
- 绑定值的模式
#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("yes");
1
},
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quart from {:#?}!", state);
25
},
}
}
fn main() {}
-
fn main() { let five = Some(5); } fn plus_one(x: Option<i32>) -> Option<i32> { match x { None => None, Some(i) => Some(i + 1), } }
- match匹配必须穷举所有的可能:
_通配符:替代其余没列出的值
fn main() {
let v = 0u8;
match v {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}
}
- if let:处理只关心一种匹配而忽略其他匹配的情况 ,放弃了穷举的可能(下面两种情况相同):
fn main() {
let v = Some(0u8);
match v {
Some(1) => println!("one"),
_ => (),
}
if let Some(3) = v {
println!("three");
}
}
- 搭配else
fn main() {
let v = Some(0u8);
match v {
Some(3) => println!("one"),
_ => (),
}
if let Some(3) = v {
println!("three");
} else {
println!("others");
}
}