定義
enum IpAddrkind {
V4,
V6,
}
struct IpAddr {
kind: IpAddrkind,
address: String,
}
使用
注意枚舉的成員位于其標識符的命名空間中靶草,并使用兩個冒號分開。
let four = IpAddrkind::V4;
let six = IpAddrkind::V6;
let home = IpAddr {
kind: IpAddrkind::V4,
address: String::from("127.0.0.1"),
};
let loopback = IpAddr {
kind: IpAddrkind::V6,
address: String::from("::1"),
};
更簡潔的方式
enum IpAddress {
V4(String),
V6(String),
}
let home = IpAddress::V4(String::from("127.0.0.1"));
let loopback = IpAddress::V6(String::from("::1"));
優(yōu)化版本岳遥,讓代碼有更多的意義
enum Ip {
V4(u8, u8, u8, u8),
V6(String),
}
let home = Ip::V4(127, 0, 0, 0);
let loopback = Ip::V6(String::from("::1"));
嵌入多種類型
// Quit 沒有關(guān)聯(lián)任何數(shù)據(jù)奕翔。
// Move 包含一個匿名結(jié)構(gòu)體。
// Write 包含單獨一個 String浩蓉。
// ChangeColor 包含三個 i32派继。
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
let m = Message::Write(String::from("hello"));
m.call();
// 結(jié)構(gòu)體和枚舉還有另一個相似點:就像可以使用 impl 來為結(jié)構(gòu)體定義方法那樣,也可以在枚舉上定義方法捻艳。
impl Message {
fn call(&self) {}
}
Option 枚舉和其相對于空值的優(yōu)勢
Rust 中沒有空值驾窟,不過它確實擁有一個可以編碼存在或不存在概念的枚舉。這個枚舉是 Option<T>认轨,而且它定義于標準庫中
let some_number = Some(5);
let some_string = Some("a string");
let absent_number: Option<i32> = None;
println!("{:?}, {:?}, {:?}", some_number, some_string, absent_number);
match 控制流運算符
let res = value_in_cents(Coin::Penny);
println!("res:{:?}", res);
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("Lucky penny!");
1
}
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
綁定值的模式
let res = value_in_cents1(Coin1::Quarter(UsState::Alabama));
println!("res = {}", res);
fn value_in_cents1(coin: Coin1) -> u8 {
match coin {
Coin1::Penny => {
println!("Lucky penny!");
1
}
Coin1::Nickel => 5,
Coin1::Dime => 10,
Coin1::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
}
}
}
#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
}
enum Coin1 {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
匹配 Option<T>
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
println!("five = {:?}, six = {:?}, none = {:?}", five, six, none);
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
_ 通配符
可以使用特殊的模式 _ 替代 默認
let some_u8_value = 0u8;
match some_u8_value {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => println!("Ignore"),
}
if let 簡單控制流
let some_u8_value = Some(0u8);
println!("{:?}", some_u8_value);
match some_u8_value {
Some(3) => println!("theree"),
_ => (),
}
簡潔寫法
if let Some(3) = some_u8_value {
println!("Three");
} else {
println!("Default");
}
可以認為 if let 是 match 的一個語法糖
let coin = Coin1::Quarter(UsState::Alabama);
let mut count = 0;
match coin {
Coin1::Quarter(state) => println!("State quarter from {:?}!", state),
_ => count += 1,
}
用if let 寫法
let coin = Coin1::Quarter(UsState::Alaska);
if let Coin1::Quarter(state) = coin {
println!("State quarter from {:?}!", state);
} else {
count += 1;
}