refactor(part5): 替换原有枚举示例为Status枚举

更新了part5的main.rs代码,移除了Direction和IpAddr枚举,改用Status枚举作为演示示例;同时补充了枚举相关练习题的参考解答代码。
This commit is contained in:
Yuhang Wu 2026-07-22 17:55:35 +08:00
parent 9dae1045ac
commit 3158ec4ebd
2 changed files with 163 additions and 15 deletions

View File

@ -89,6 +89,8 @@ fn main() {
}
```
> 可以通过
### 题目 1-5
```rust
@ -99,6 +101,8 @@ fn main() {
}
```
> 不能Option枚举类型不能直接用+需要实现add方法
### 题目 1-6
```rust
@ -122,6 +126,8 @@ fn main() {
}
```
> 不能match需要匹配所有可能出现的类型
### 题目 1-7
```rust
@ -139,6 +145,8 @@ fn main() {
}
```
> 可以
### 题目 1-8
```rust
@ -161,6 +169,8 @@ fn main() {
}
```
> 可以
***
## 二、填空题:补充代码
@ -188,6 +198,23 @@ fn main() {
}
```
```rust
enum TrafficLight {
Red,
Yellow,
Green,
}
fn main() {
let light = TrafficLight::Red;
match light {
TrafficLight::Red => println!("停止"),
TrafficLight::Yellow => println!("减速"),
TrafficLight::Green => println!("通行"),
}
}
```
### 题目 2-2带数据的枚举
```rust
@ -209,6 +236,21 @@ fn main() {
}
```
```rust
enum WebEvent {
PageLoad,
KeyPress(char),
Click { x: i32, y: i32 },
}
fn main() {
let load = WebEvent::PageLoad;
let key = WebEvent::KeyPress('a');
let click = WebEvent::Click { x: 100, y: 200 };
println!("ok");
}
```
### 题目 2-3Option 的使用
```rust
@ -230,6 +272,23 @@ fn main() {
}
```
```rust
fn main() {
let v1 = Some(10);
let v2: Option<i32> = None;
let val1 = match v1 {
Some(n) => n,
None => 0,
};
let val2 = match v2 {
Some(n) => n,
None => 0,
};
println!("val1: {}, val2: {}", val1, val2);
}
```
### 题目 2-4Result 的使用
```rust
@ -261,6 +320,31 @@ fn main() {
}
```
```rust
fn divide(a: i32, b: i32) -> Result<i32, String> {
if b == 0 {
Err(String::from("除数不能为零"))
} else {
Ok(a / b)
}
}
fn main() {
let result1 = divide(10, 2);
let result2 = divide(10, 0);
match result1 {
Ok(val) => println!("结果: {}", val),
Err(e) => println!("错误: {}", e),
}
match result2 {
Ok(val) => println!("结果: {}", val),
Err(e) => println!("错误: {}", e),
}
}
```
### 题目 2-5枚举上的方法
```rust
@ -290,6 +374,32 @@ fn main() {
}
```
```rust
enum Message {
Quit,
Write(String),
Move { x: i32, y: i32 },
}
impl Message {
fn call(&self) {
match self {
Message::Quit => println!("退出"),
Message::Write(text) => println!("写入: {}", text),
Message::Move { x, y } => println!("移动到 ({}, {})", x, y),
}
}
}
fn main() {
let m = Message::Write(String::from("你好世界"));
m.call();
let n = Message::Move { x: 10, y: 20 };
n.call();
}
```
### 题目 2-6if let 语法
```rust
@ -310,6 +420,16 @@ fn main() {
}
```
```rust
fn main() {
let config_max = Some(3u8);
if let Some(max) = config_max {
println!("最大值为: {}", max);
}
}
```
### 题目 2-7嵌套 Option 处理
```rust
@ -326,6 +446,18 @@ fn main() {
}
```
```rust
fn main() {
let opt: Option<Option<i32>> = Some(Some(42));
match opt {
Some(Some(inner)) => println!("值是: {}", inner),
Some(None) => println!("内层是 None"),
None => println!("外层是 None"),
}
}
```
### 题目 2-8match 的多种模式
```rust
@ -342,6 +474,18 @@ fn main() {
}
```
```rust
fn main() {
let x = 3;
match x {
1 | 2 => println!("是 1 或 2"),
3..=8 => println!("在 3 到 8 之间"),
_ => println!("其他值"),
}
}
```
***
## 三、找出并修复错误
@ -362,6 +506,19 @@ fn main() {
}
```
```rust
#[derive(Debug)]
enum Status {
Active,
Inactive,
}
fn main() {
let s = Status::Active;
println!("{:?}", s);
}
```
### 题目 3-2
```rust

View File

@ -1,19 +1,10 @@
#[derive(Debug)]
enum Direction {
North,
South,
East,
West,
}
enum IpAddr {
V4(String),
V6(String),
enum Status {
Active,
Inactive,
}
fn main() {
let direction = Direction::North;
println!(" direction: {:?}", direction);
let home = IpAddr::V4(String::from("127.0.0.1"));
// println!(" home: {:?}", home);
let s = Status::Active;
println!("{:?}", s);
}