2.4 结构体、枚举与模式匹配
结构体、枚举和模式匹配是 Rust 中用于组织数据和编写清晰、简洁代码的强大工具。它们允许开发者定义自定义数据类型,并根据数据的结构和内容执行不同的操作。
2.4.1 结构体
结构体是 Rust 中用于将多个相关数据项组合成一个自定义数据类型的方式。结构体使用 struct 关键字定义,其基本语法如下:
1
2
3
4
5
|
struct StructName {
field1: type1,
field2: type2,
// ...
}
|
StructName 是结构体的名称,遵循 Rust 的命名规范。
field1, field2 是结构体的字段,每个字段都需要指定类型。
1. 实例化结构体:
可以使用结构体名称和字段值来创建结构体实例:
1
2
3
4
5
|
let instance = StructName {
field1: value1,
field2: value2,
// ...
};
|
2. 访问结构体字段:
可以使用点号 (.) 访问结构体实例的字段:
1
|
println!("{}", instance.field1);
|
3. 结构体更新语法:
可以使用结构体更新语法来创建新的结构体实例,并继承现有实例的部分字段值:
1
2
3
4
|
let instance2 = StructName {
field1: value3,
..instance
};
|
4. 元组结构体:
元组结构体是一种类似于元组的结构体,其字段没有名称,只有类型:
1
2
3
|
struct Color(i32, i32, i32);
let black = Color(0, 0, 0);
|
5. 单元结构体:
单元结构体是一种没有任何字段的结构体:
1
2
3
|
struct UnitStruct;
let instance = UnitStruct;
|
2.4.2 枚举
枚举是 Rust 中用于定义一组可能值的类型。枚举使用 enum 关键字定义,其基本语法如下:
1
2
3
4
5
|
enum EnumName {
Variant1,
Variant2,
// ...
}
|
EnumName 是枚举的名称,遵循 Rust 的命名规范。
Variant1, Variant2 是枚举的变体,每个变体可以包含数据。
1. 枚举变体:
枚举变体可以包含数据,例如:
1
2
3
4
5
6
|
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
|
2. 匹配枚举:
可以使用 match 表达式来匹配枚举变体并执行相应的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
let msg = Message::Write(String::from("hello"));
match msg {
Message::Quit => {
println!("The Quit variant has no data.");
}
Message::Move { x, y } => {
println!("Move to x: {}, y: {}", x, y);
}
Message::Write(text) => {
println!("Text message: {}", text);
}
Message::ChangeColor(r, g, b) => {
println!("Change color to red: {}, green: {}, blue: {}", r, g, b);
}
}
|
2.4.3 模式匹配
模式匹配是 Rust 中一种强大的机制,用于根据数据的结构和内容执行不同的操作。模式匹配可以使用 match 表达式、if let 表达式和 while let 表达式来实现。
1. match 表达式:
match 表达式用于将一个值与一系列模式进行比较,并执行与第一个匹配的模式对应的代码:
1
2
3
4
5
6
|
let value = Some(5);
match value {
Some(x) => println!("Got a value: {}", x),
None => println!("Got nothing"),
}
|
2. if let 表达式:
if let 表达式用于简化只关心一种模式的 match 表达式:
1
2
3
4
5
|
let value = Some(5);
if let Some(x) = value {
println!("Got a value: {}", x);
}
|
3. while let 表达式:
while let 表达式用于循环匹配模式,直到模式不匹配为止:
1
2
3
4
5
6
7
8
9
|
let mut stack = Vec::new();
stack.push(1);
stack.push(2);
stack.push(3);
while let Some(top) = stack.pop() {
println!("{}", top);
}
|
2.4.4 代码实例
1. 结构体:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main() {
let user1 = User {
username: String::from("someusername123"),
email: String::from("someone@example.com"),
sign_in_count: 1,
active: true,
};
println!("Username: {}", user1.username);
println!("Email: {}", user1.email);
println!("Sign-in count: {}", user1.sign_in_count);
println!("Active: {}", user1.active);
}
|
2. 枚举:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
enum IpAddr {
V4(u8, u8, u8, u8),
V6(String),
}
fn main() {
let home = IpAddr::V4(127, 0, 0, 1);
let loopback = IpAddr::V6(String::from("::1"));
match home {
IpAddr::V4(a, b, c, d) => println!("Home IP: {}.{}.{}.{}", a, b, c, d),
IpAddr::V6(s) => println!("Home IP: {}", s),
}
match loopback {
IpAddr::V4(a, b, c, d) => println!("Loopback IP: {}.{}.{}.{}", a, b, c, d),
IpAddr::V6(s) => println!("Loopback IP: {}", s),
}
}
|
3. 模式匹配:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
fn main() {
let some_value = Some(3);
match some_value {
Some(3) => println!("three"),
Some(x) => println!("Some other value: {}", x),
None => println!("None"),
}
if let Some(3) = some_value {
println!("three");
}
let mut stack = Vec::new();
stack.push(1);
stack.push(2);
stack.push(3);
while let Some(top) = stack.pop() {
println!("{}", top);
}
}
|
2.4.5 总结
结构体、枚举和模式匹配是 Rust 中用于组织数据和编写清晰、简洁代码的强大工具。结构体允许开发者定义自定义数据类型,枚举允许开发者定义一组可能值,而模式匹配则允许开发者根据数据的结构和内容执行不同的操作。掌握结构体、枚举和模式匹配的使用技巧,可以编写出更优雅、更易维护的 Rust 代码。
以下是一些学习 Rust 结构体、枚举和模式匹配的资源: