Rust编程语言之枚举与模式匹配一、定义枚举枚举枚举允许我们列举所有可能的值来定义一个类型定义枚举IP地址:IPv4、IPv6enumIpAddrKing{V4,V6,}枚举值例子:letfour=IpAddrKind::V4;letsix
enum IpAddrKing {
V4,
V6,
}
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
enum IpAddrKind {
V4,
V6,
}
fn main() {
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
route(four);
route(six);
route(IpAddrKind::V6);
}
fn route(ip_kind: IpAddrKind) {}
enum IpAddr {
V4(String),
V6(String),
}
未使用前:
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"),
};
}
enum IpAddr {
V4(u8, u8, u8, u8),
V6(String),
}
使用之后:
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"));
}
struct Ipv4Addr {
// --snip--
}
struct Ipv6Addr {
// --snip--
}
enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}
例子:
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);
}
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();
}
enum Option<T> {Some(T), None,}
Option<T>
Some(T)
None
fn main() {
let some_number = Some(5);
let some_string = Some("A String");
let absent_number: Option<i32> = None;
}
Option<T>
比 Null 好在哪?Option<T>
和T 是不同的类型,不可以把 Option<T>
直接当成 TOption<T>
中的 T,必须将它转换为 Tstring a = null;
string b = a + "12345";
fn main() {
let x: i8 = 5;
let y: Option<i8> = Some(5);
let sum = x + y; // 报错 需要转换
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
fn main() {
println!("Hello, world!");
}
#[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!("Penny!");
1
}
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from: {:?}!", state);
25
}
}
}
fn main() {
let c = Coin::Quarter(UsState::Alaska);
println!("{}", value_in_cents(c));
}
Option<T>
fn main() {
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
}
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
fn main() {
let v = 0u8;
match v {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}
}
fn main() {
let v = Some(0u8);
match v {
Some(3) => println!("three"),
_ => (),
}
if let Some(3) = v {
println!("three");
}
}
fn main() {
let v = Some(0u8);
match v {
Some(3) => println!("three"),
_ => println!("others"),
}
if let Some(3) = v {
println!("three");
} else {
println!("others");
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!