练习
练习 1:类型安全状态机 ★★(约 30 分钟)
使用 type-state 模式构建一个交通灯状态机。灯必须按 Red → Green → Yellow → Red 转换,其他顺序都不应该可能。
🔑 解答
use std::marker::PhantomData;
struct Red;
struct Green;
struct Yellow;
struct TrafficLight<State> {
_state: PhantomData<State>,
}
impl TrafficLight<Red> {
fn new() -> Self {
println!("🔴 Red — STOP");
TrafficLight { _state: PhantomData }
}
fn go(self) -> TrafficLight<Green> {
println!("🟢 Green — GO");
TrafficLight { _state: PhantomData }
}
}
impl TrafficLight<Green> {
fn caution(self) -> TrafficLight<Yellow> {
println!("🟡 Yellow — CAUTION");
TrafficLight { _state: PhantomData }
}
}
impl TrafficLight<Yellow> {
fn stop(self) -> TrafficLight<Red> {
println!("🔴 Red — STOP");
TrafficLight { _state: PhantomData }
}
}
fn main() {
let light = TrafficLight::new(); // Red
let light = light.go(); // Green
let light = light.caution(); // Yellow
let light = light.stop(); // Red
// light.caution(); // ❌ 编译错误:Red 上没有方法 `caution`
// TrafficLight::new().stop(); // ❌ 编译错误:Red 上没有方法 `stop`
}
关键要点:无效的转换是编译错误,而非运行时 panic。
练习 2:带 PhantomData 的测量单位 ★★(约 30 分钟)
扩展第 4 章的测量单位模式以支持:
Meters、Seconds、Kilograms- 相同单位的加法
- 乘法:
Meters * Meters = SquareMeters - 除法:
Meters / Seconds = MetersPerSecond
🔑 解答
use std::marker::PhantomData;
use std::ops::{Add, Mul, Div};
#[derive(Clone, Copy)]
struct Meters;
#[derive(Clone, Copy)]
struct Seconds;
#[derive(Clone, Copy)]
struct Kilograms;
#[derive(Clone, Copy)]
struct SquareMeters;
#[derive(Clone, Copy)]
struct MetersPerSecond;
#[derive(Debug, Clone, Copy)]
struct Qty<U> {
value: f64,
_unit: PhantomData<U>,
}
impl<U> Qty<U> {
fn new(v: f64) -> Self { Qty { value: v, _unit: PhantomData } }
}
impl<U> Add for Qty<U> {
type Output = Qty<U>;
fn add(self, rhs: Self) -> Self::Output { Qty::new(self.value + rhs.value) }
}
impl Mul<Qty<Meters>> for Qty<Meters> {
type Output = Qty<SquareMeters>;
fn mul(self, rhs: Qty<Meters>) -> Qty<SquareMeters> {
Qty::new(self.value * rhs.value)
}
}
impl Div<Qty<Seconds>> for Qty<Meters> {
type Output = Qty<MetersPerSecond>;
fn div(self, rhs: Qty<Seconds>) -> Qty<MetersPerSecond> {
Qty::new(self.value / rhs.value)
}
}
fn main() {
let width = Qty::<Meters>::new(5.0);
let height = Qty::<Meters>::new(3.0);
let area = width * height; // Qty<SquareMeters>
println!("Area: {:.1} m²", area.value);
let dist = Qty::<Meters>::new(100.0);
let time = Qty::<Seconds>::new(9.58);
let speed = dist / time;
println!("Speed: {:.2} m/s", speed.value);
let sum = width + height; // 相同单位 ✅
println!("Sum: {:.1} m", sum.value);
// let bad = width + time; // ❌ 编译错误:不能相加 Meters + Seconds
}
练习 3:基于通道的 worker 池 ★★★(约 45 分钟)
使用通道构建一个 worker 池,其中:
- 调度器通过通道发送
Job结构体 - N 个 worker 消费作业并发送结果回来
- 使用
crossbeam-channel(如果不可用则使用std::sync::mpsc)
🔑 解答
use std::sync::mpsc;
use std::thread;
struct Job {
id: u64,
data: String,
}
struct JobResult {
job_id: u64,
output: String,
worker_id: usize,
}
fn worker_pool(jobs: Vec<Job>, num_workers: usize) -> Vec<JobResult> {
let (job_tx, job_rx) = mpsc::channel::<Job>();
let (result_tx, result_rx) = mpsc::channel::<JobResult>();
// 在 Arc<Mutex> 中包装接收器以便在 worker 之间共享
let job_rx = std::sync::Arc::new(std::sync::Mutex::new(job_rx));
// 生成 worker
let mut handles = Vec::new();
for worker_id in 0..num_workers {
let job_rx = job_rx.clone();
let result_tx = result_tx.clone();
handles.push(thread::spawn(move || {
loop {
// 锁定、接收、解锁 —— 短临界区
let job = {
let rx = job_rx.lock().unwrap();
rx.recv() // 阻塞直到有作业或通道关闭
};
match job {
Ok(job) => {
let output = format!("processed '{}' by worker {worker_id}", job.data);
result_tx.send(JobResult {
job_id: job.id,
output,
worker_id,
}).unwrap();
}
Err(_) => break, // 通道关闭 —— 退出
}
}
}));
}
drop(result_tx); // 丢弃我们的副本以便结果通道在 worker 完成时关闭
// 分发作业
let num_jobs = jobs.len();
for job in jobs {
job_tx.send(job).unwrap();
}
drop(job_tx); // 关闭作业通道 —— worker 在 draining 后退出
// 收集结果
let mut results = Vec::new();
for result in result_rx {
results.push(result);
}
assert_eq!(results.len(), num_jobs);
for h in handles { h.join().unwrap(); }
results
}
fn main() {
let jobs: Vec<Job> = (0..20).map(|i| Job {
id: i,
data: format!("task-{i}"),
}).collect();
let results = worker_pool(jobs, 4);
for r in &results {
println!("[worker {}] job {}: {}", r.worker_id, r.job_id, r.output);
}
}
练习 4:高阶组合器管道 ★★(约 25 分钟)
创建一个 Pipeline 结构体来链接转换。它应该支持 .pipe(f) 添加转换和 .execute(input) 运行完整链。
🔑 解答
struct Pipeline<T> {
transforms: Vec<Box<dyn Fn(T) -> T>>,
}
impl<T: 'static> Pipeline<T> {
fn new() -> Self {
Pipeline { transforms: Vec::new() }
}
fn pipe(mut self, f: impl Fn(T) -> T + 'static) -> Self {
self.transforms.push(Box::new(f));
self
}
fn execute(self, input: T) -> T {
self.transforms.into_iter().fold(input, |val, f| f(val))
}
}
fn main() {
let result = Pipeline::new()
.pipe(|s: String| s.trim().to_string())
.pipe(|s| s.to_uppercase())
.pipe(|s| format!(">>> {s} <<<"))
.execute(" hello world ".to_string());
println!("{result}"); // >>> HELLO WORLD <<<
// 数字管道:
let result = Pipeline::new()
.pipe(|x: i32| x * 2)
.pipe(|x| x + 10)
.pipe(|x| x * x)
.execute(5);
println!("{result}"); // (5*2 + 10)^2 = 400
}
Bonus:在阶段之间改变类型的通用管道将使用不同的设计 —— 每个 .pipe() 返回一个具有不同输出类型的 Pipeline(这需要更高级的泛型处理)。
练习 5:带 thiserror 的错误层次 ★★(约 30 分钟)
为文件处理应用程序设计错误类型层次结构,该程序可能在 I/O、解析(JSON 和 CSV)和验证期间失败。使用 thiserror 并演示 ? 传播。
🔑 解答
use thiserror::Error;
#[derive(Error, Debug)]
pub enum AppError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("JSON parse error: {0}")]
Json(#[from] serde_json::Error),
#[error("CSV error at line {line}: {message}")]
Csv { line: usize, message: String },
#[error("validation error: {field} — {reason}")]
Validation { field: String, reason: String },
}
fn read_file(path: &str) -> Result<String, AppError> {
Ok(std::fs::read_to_string(path)?) // io::Error → AppError::Io via #[from]
}
fn parse_json(content: &str) -> Result<serde_json::Value, AppError> {
Ok(serde_json::from_str(content)?) // serde_json::Error → AppError::Json
}
fn validate_name(value: &serde_json::Value) -> Result<String, AppError> {
let name = value.get("name")
.and_then(|v| v.as_str())
.ok_or_else(|| AppError::Validation {
field: "name".into(),
reason: "must be a non-null string".into(),
})?;
if name.is_empty() {
return Err(AppError::Validation {
field: "name".into(),
reason: "must not be empty".into(),
});
}
Ok(name.to_string())
}
fn process_file(path: &str) -> Result<String, AppError> {
let content = read_file(path)?;
let json = parse_json(&content)?;
let name = validate_name(&json)?;
Ok(name)
}
fn main() {
match process_file("config.json") {
Ok(name) => println!("Name: {name}"),
Err(e) => eprintln!("Error: {e}"),
}
}
练习 6:带关联类型的泛型 trait ★★★(约 40 分钟)
设计一个带有 Error 和 Id 关联类型的 Repository<T> trait。为内存存储实现它并演示编译时类型安全。
🔑 解答
use std::collections::HashMap;
trait Repository {
type Item;
type Id;
type Error;
fn get(&self, id: &Self::Id) -> Result<Option<&Self::Item>, Self::Error>;
fn insert(&mut self, item: Self::Item) -> Result<Self::Id, Self::Error>;
fn delete(&mut self, id: &Self::Id) -> Result<bool, Self::Error>;
}
#[derive(Debug, Clone)]
struct User {
name: String,
email: String,
}
struct InMemoryUserRepo {
data: HashMap<u64, User>,
next_id: u64,
}
impl InMemoryUserRepo {
fn new() -> Self {
InMemoryUserRepo { data: HashMap::new(), next_id: 1 }
}
}
// 错误类型是 Infallible —— 内存操作从失败
impl Repository for InMemoryUserRepo {
type Item = User;
type Id = u64;
type Error = std::convert::Infallible;
fn get(&self, id: &u64) -> Result<Option<&User>, Self::Error> {
Ok(self.data.get(id))
}
fn insert(&mut self, item: User) -> Result<u64, Self::Error> {
let id = self.next_id;
self.next_id += 1;
self.data.insert(id, item);
Ok(id)
}
fn delete(&mut self, id: &u64) -> Result<bool, Self::Error> {
Ok(self.data.remove(id).is_some())
}
}
// 泛型函数适用于 ANY repository:
fn create_and_fetch<R: Repository>(repo: &mut R, item: R::Item) -> Result<(), R::Error>
where
R::Item: std::fmt::Debug,
R::Id: std::fmt::Debug,
{
let id = repo.insert(item)?;
println!("Inserted with id: {id:?}");
let retrieved = repo.get(&id)?;
println!("Retrieved: {retrieved:?}");
Ok(())
}
fn main() {
let mut repo = InMemoryUserRepo::new();
create_and_fetch(&mut repo, User {
name: "Alice".into(),
email: "alice@example.com".into(),
}).unwrap();
}
练习 7:Unsafe 之上的安全包装器(第 11 章)★★★(约 45 分钟)
编写一个 FixedVec<T, const N: usize> —— 固定容量、栈分配的向量。
要求:
push(&mut self, value: T) -> Result<(), T>满时返回Err(value)pop(&mut self) -> Option<T>返回并移除最后一个元素as_slice(&self) -> &[T]借用已初始化的元素- 所有公共方法必须是安全的;所有 unsafe 必须用
SAFETY:注释封装 Drop必须清理已初始化的元素
Hint:使用 MaybeUninit<T> 和 [const { MaybeUninit::uninit() }; N]。
🔑 解答
use std::mem::MaybeUninit;
pub struct FixedVec<T, const N: usize> {
data: [MaybeUninit<T>; N],
len: usize,
}
impl<T, const N: usize> FixedVec<T, N> {
pub fn new() -> Self {
FixedVec {
data: [const { MaybeUninit::uninit() }; N],
len: 0,
}
}
pub fn push(&mut self, value: T) -> Result<(), T> {
if self.len >= N { return Err(value); }
// SAFETY: len < N,所以 data[len] 在边界内。
self.data[self.len] = MaybeUninit::new(value);
self.len += 1;
Ok(())
}
pub fn pop(&mut self) -> Option<T> {
if self.len == 0 { return None; }
self.len -= 1;
// SAFETY: data[len] 已初始化(递减前 len > 0)。
Some(unsafe { self.data[self.len].assume_init_read() })
}
pub fn as_slice(&self) -> &[T] {
// SAFETY: data[0..len] 都已初始化,且 MaybeUninit<T>
// 与 T 有相同的布局。
unsafe { std::slice::from_raw_parts(self.data.as_ptr() as *const T, self.len) }
}
pub fn len(&self) -> usize { self.len }
pub fn is_empty(&self) -> bool { self.len == 0 }
}
impl<T, const N: usize> Drop for FixedVec<T, N> {
fn drop(&mut self) {
// SAFETY: data[0..len] 都已初始化 —— drop 每一个。
for i in 0..self.len {
unsafe { self.data[i].assume_init_drop(); }
}
}
}
fn main() {
let mut v = FixedVec::<String, 4>::new();
v.push("hello".into()).unwrap();
v.push("world".into()).unwrap();
assert_eq!(v.as_slice(), &["hello", "world"]);
assert_eq!(v.pop(), Some("world".into()));
assert_eq!(v.len(), 1);
// Drop 清理剩余的 "hello"
}
练习 8:声明式宏 —— map!(第 12 章)★(约 15 分钟)
编写一个 map! 宏,从键值对创建 HashMap,类似于 vec![]:
#![allow(unused)]
fn main() {
let m = map! {
"host" => "localhost",
"port" => "8080",
};
assert_eq!(m.get("host"), Some(&"localhost"));
assert_eq!(m.len(), 2);
}
要求:
- 支持尾随逗号
- 支持空调用
map!{} - 适用于任何为
Into<K>和Into<V>实现类型的类型以获得最大灵活性
🔑 解答
macro_rules! map {
// 空情况
() => {
std::collections::HashMap::new()
};
// 一个或多个 key => value 对(尾随逗号可选)
( $( $key:expr => $val:expr ),+ $(,)? ) => {{
let mut m = std::collections::HashMap::new();
$( m.insert($key, $val); )+
m
}};
}
fn main() {
// 基本使用:
let config = map! {
"host" => "localhost",
"port" => "8080",
"timeout" => "30",
};
assert_eq!(config.len(), 3);
assert_eq!(config["host"], "localhost");
// 空 map:
let empty: std::collections::HashMap<String, String> = map!();
assert!(empty.is_empty());
// 不同类型:
let scores = map! {
1 => 100,
2 => 200,
};
assert_eq!(scores[&1], 100);
}
练习 9:自定义 serde 反序列化(第 10 章)★★★(约 45 分钟)
设计一个 Duration 包装器,使用自定义 serde 反序列化器从人类可读的字符串(如 "30s"、"5m"、"2h")反序列化。结构体还应该序列化回相同格式。
🔑 解答
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
struct HumanDuration(std::time::Duration);
impl HumanDuration {
fn from_str(s: &str) -> Result<Self, String> {
let s = s.trim();
if s.is_empty() { return Err("empty duration string".into()); }
let (num_str, suffix) = s.split_at(
s.find(|c: char| !c.is_ascii_digit()).unwrap_or(s.len())
);
let value: u64 = num_str.parse()
.map_err(|_| format!("invalid number: {num_str}"))?;
let duration = match suffix {
"s" | "sec" => std::time::Duration::from_secs(value),
"m" | "min" => std::time::Duration::from_secs(value * 60),
"h" | "hr" => std::time::Duration::from_secs(value * 3600),
"ms" => std::time::Duration::from_millis(value),
other => return Err(format!("unknown suffix: {other}")),
};
Ok(HumanDuration(duration))
}
}
impl fmt::Display for HumanDuration {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let secs = self.0.as_secs();
if secs == 0 {
write!(f, "{}ms", self.0.as_millis())
} else if secs % 3600 == 0 {
write!(f, "{}h", secs / 3600)
} else if secs % 60 == 0 {
write!(f, "{}m", secs / 60)
} else {
write!(f, "{}s", secs)
}
}
}
impl Serialize for HumanDuration {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for HumanDuration {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
HumanDuration::from_str(&s).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Deserialize, Serialize)]
struct Config {
timeout: HumanDuration,
retry_interval: HumanDuration,
}
fn main() {
let json = r#"{ "timeout": "30s", "retry_interval": "5m" }"#;
let config: Config = serde_json::from_str(json).unwrap();
assert_eq!(config.timeout.0, std::time::Duration::from_secs(30));
assert_eq!(config.retry_interval.0, std::time::Duration::from_secs(300));
// 正确往返:
let serialized = serde_json::to_string(&config).unwrap();
assert!(serialized.contains("30s"));
assert!(serialized.contains("5m"));
println!("Config: {serialized}");
}
练习 10 —— 带超时的并发抓取器 ★★(约 25 分钟)
编写一个 async 函数 fetch_all,生成三个 tokio::spawn 任务,每个模拟使用 tokio::time::sleep 的网络调用。使用 tokio::try_join! 连接所有三个,并用 tokio::time::timeout(Duration::from_secs(5), ...) 包装。返回 Result<Vec<String>, ...> 或如果任何任务失败或截止日期过期则返回错误。
学习目标:tokio::spawn、try_join!、timeout、跨任务边界的错误传播。
提示
每个生成的任务返回 Result<String, _>。try_join! 解包所有三个。
将整个 try_join! 包装在 timeout() 中 —— Elapsed 错误意味着你达到了截止日期。
解答
use tokio::time::{sleep, timeout, Duration};
async fn fake_fetch(name: &'static str, delay_ms: u64) -> Result<String, String> {
sleep(Duration::from_millis(delay_ms)).await;
Ok(format!("{name}: OK"))
}
async fn fetch_all() -> Result<Vec<String>, Box<dyn std::error::Error>> {
let deadline = Duration::from_secs(5);
let (a, b, c) = timeout(deadline, async {
let h1 = tokio::spawn(fake_fetch("svc-a", 100));
let h2 = tokio::spawn(fake_fetch("svc-b", 200));
let h3 = tokio::spawn(fake_fetch("svc-c", 150));
tokio::try_join!(h1, h2, h3)
})
.await??; // 第一个 ? = timeout, 第二个 ? = join
Ok(vec![a?, b?, c?]) // 解包内部 Results
}
#[tokio::main]
async fn main() {
let results = fetch_all().await.unwrap();
for r in &results {
println!("{r}");
}
}
练习 11 —— Async 通道管道 ★★★(约 40 分钟)
使用 tokio::sync::mpsc 构建 producer → transformer → consumer 管道:
- Producer:发送整数 1..=20 到通道 A(容量 4)。
- Transformer:从通道 A 读取,平方每个值,发送到通道 B。
- Consumer:从通道 B 读取,收集到
Vec<u64>,返回它。
所有三个阶段作为并发 tokio::spawn 任务运行。使用有界通道演示背压。断言最终 vec 等于 [1, 4, 9, ..., 400]。
学习目标:mpsc::channel、有界背压、带 move 闭包的 tokio::spawn、通过通道关闭实现优雅关闭。
解答
use tokio::sync::mpsc;
#[tokio::main]
async fn main() {
let (tx_a, mut rx_a) = mpsc::channel::<u64>(4); // 有界 —— 背压
let (tx_b, mut rx_b) = mpsc::channel::<u64>(4);
// Producer
let producer = tokio::spawn(async move {
for i in 1..=20u64 {
tx_a.send(i).await.unwrap();
}
// tx_a 在这里 drop —— 通道 A 关闭
});
// Transformer
let transformer = tokio::spawn(async move {
while let Some(val) = rx_a.recv().await {
tx_b.send(val * val).await.unwrap();
}
// tx_b 在这里 drop —— 通道 B 关闭
});
// Consumer
let consumer = tokio::spawn(async move {
let mut results = Vec::new();
while let Some(val) = rx_b.recv().await {
results.push(val);
}
results
});
producer.await.unwrap();
transformer.await.unwrap();
let results = consumer.await.unwrap();
let expected: Vec<u64> = (1..=20).map(|x: u64| x * x).collect();
assert_eq!(results, expected);
println!("Pipeline complete: {results:?}");
}