阶段三:进阶(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. 异步网络爬虫
  • 异步请求网页
  • 并发抓取多个页面
  • 提取信息并保存
相关推荐
东东5161 小时前
基于ssm的网上房屋中介管理系统vue
前端·javascript·vue.js
harrain2 小时前
什么!vue3.4开始,v-model不能用在prop上
前端·javascript·vue.js
fanruitian8 小时前
uniapp android开发 测试板本与发行版本
前端·javascript·uni-app
rayufo8 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk8 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
2501_944525549 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
李白你好10 小时前
Burp Suite插件用于自动检测Web应用程序中的未授权访问漏洞
前端
刘一说11 小时前
Vue 组件不必要的重新渲染问题解析:为什么子组件总在“无故”刷新?
前端·javascript·vue.js
徐同保12 小时前
React useRef 完全指南:在异步回调中访问最新的 props/state引言
前端·javascript·react.js
刘一说13 小时前
Vue 导航守卫未生效问题解析:为什么路由守卫不执行或逻辑失效?
前端·javascript·vue.js