阶段三:进阶(Rust 高级特性)

目标

掌握泛型、特征(Trait)、智能指针、并发和异步编程。


1. 泛型与特征(Trait)

泛型函数

ini 复制代码
fn largest<T: PartialOrd>(list: &[T]) -> &T {
    let mut largest = &list[0];
    for item in list {
        if item > largest {
            largest = item;
        }
    }
    largest
}

fn main() {
    let numbers = vec![34, 50, 25, 100, 65];
    println!("最大值: {}", largest(&numbers));
}

泛型结构体

ini 复制代码
struct Point<T> {
    x: T,
    y: T,
}

fn main() {
    let p1 = Point { x: 5, y: 10 };
    let p2 = Point { x: 1.0, y: 4.0 };
}

Trait 定义与实现

rust 复制代码
trait Summary {
    fn summarize(&self) -> String;
}

struct NewsArticle {
    headline: String,
    author: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("{} by {}", self.headline, self.author)
    }
}

fn main() {
    let article = NewsArticle { headline: "Rust 进阶".to_string(), author: "Eric".to_string() };
    println!("{}", article.summarize());
}

Trait bound 与 impl Trait

rust 复制代码
fn notify(item: &impl Summary) {
    println!("Breaking news! {}", item.summarize());
}

常用 Trait

  • Debug:打印调试信息
  • Clone:深拷贝
  • Copy:轻量类型拷贝
  • Drop:自定义释放逻辑

2. 智能指针

Box:堆分配

css 复制代码
let b = Box::new(5);
println!("b = {}", b);

Rc:引用计数

css 复制代码
use std::rc::Rc;

let a = Rc::new(5);
let b = Rc::clone(&a);
println!("引用计数: {}", Rc::strong_count(&a));

Arc:线程安全引用计数

rust 复制代码
use std::sync::Arc;
use std::thread;

let a = Arc::new(5);
let mut handles = vec![];

for _ in 0..3 {
    let a = Arc::clone(&a);
    let handle = thread::spawn(move || {
        println!("{}", a);
    });
    handles.push(handle);
}

for h in handles {
    h.join().unwrap();
}

RefCell:内部可变性

rust 复制代码
use std::cell::RefCell;

let x = RefCell::new(5);
* x.borrow_mut() += 1;
println!("{}", x.borrow());

3. 并发编程

线程

rust 复制代码
use std::thread;

let handle = thread::spawn(|| {
    for i in 1..5 {
        println!("子线程 {}", i);
    }
});

for i in 1..5 {
    println!("主线程 {}", i);
}

handle.join().unwrap();

Mutex 与 RwLock

ini 复制代码
use std::sync::{Arc, Mutex};
use std::thread;

let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];

for _ in 0..10 {
    let counter = Arc::clone(&counter);
    let handle = thread::spawn(move || {
        let mut num = counter.lock().unwrap();
        *num += 1;
    });
    handles.push(handle);
}

for h in handles {
    h.join().unwrap();
}

println!("结果: {}", *counter.lock().unwrap());

channels 消息传递

rust 复制代码
use std::sync::mpsc;
use std::thread;

let (tx, rx) = mpsc::channel();

thread::spawn(move || {
    tx.send("Hello from thread").unwrap();
});

println!("{}", rx.recv().unwrap());

4. 异步编程

async / await

rust 复制代码
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    println!("开始");
    sleep(Duration::from_secs(2)).await;
    println!("两秒后输出");
}

Future trait

rust 复制代码
use std::future::Future;

async fn hello() {
    println!("Hello async");
}

fn main() {
    let f: impl Future<Output=()> = hello();
    futures::executor::block_on(f);
}

tokio / async-std

  • tokio:高性能异步运行时
  • async-std:更接近标准库风格
  • 异步 I/O、HTTP 请求、数据库访问

练习建议

  1. 多线程下载器
  • 给定 URL 列表
  • 每个 URL 用一个线程下载
  • 保存到本地文件
  1. 异步网络爬虫
  • 异步请求网页
  • 并发抓取多个页面
  • 提取信息并保存
相关推荐
我不是外星人10 分钟前
一键还原任意网站,这套 Playwright + 多 Agent 工程流太猛了
前端·后端·ai编程
用户0595401744615 分钟前
AI 客服突然“失忆”,排查 6 小时发现 LangChain 记忆存储的 3 个致命坑
前端·css
IT_陈寒36 分钟前
Vue的v-if和v-show,我竟然用错了这么久
前端·人工智能·后端
云絮.39 分钟前
计算机相关工作原理
服务器·前端·数据库
随风一样自由41 分钟前
【前端+React+缓存竞态】导航切换缓存刷新机制
前端·react.js·缓存
恋猫de小郭1 小时前
Flutter 开发怎么做 Agent ?从工程实战详细给你解读下
android·前端·flutter
IT_陈寒1 小时前
React状态更新延迟搞晕我,原来是闭包在捣鬼
前端·人工智能·后端
晓得迷路了1 小时前
栗子前端技术周刊第 136 期 - pnpr、Flow 重构移植 Rust、Astryx...
前端·javascript·css
柳杉1 小时前
别再手写大屏了!用这套「3D 地图 + 拖拽脚手架」示例源码,交付效率提升 80%
前端·three.js·数据可视化
乌夷1 小时前
Vue.js 3 的设计思路
前端·javascript·vue.js