Rust - 消息传递

Rust 主要通过通道(Channel)进行消息传递,它基于"共享内存不如传递消息"的理念。Rust 中消息传递的实现方式主要有以下几种:

通道(Channel)

通道是 Rust 标准库提供的消息传递机制,分为同步通道和异步通道。其特点是发送者和接收者可以位于不同线程,实现线程间安全通信。 下面是一个使用同步通道的示例:

rust 复制代码
use std::sync::mpsc; 
use std::thread; 
fn main() { 
    // 创建一个同步通道,返回发送端和接收端 
    let (tx, rx) = mpsc::channel(); 
    // 启动一个新线程 
    thread::spawn(move || { 
        // 发送消息到通道 
        tx.send("Hello from thread!").unwrap(); 
        println!("Message sent"); 
    }); 
    // 主线程接收消息 
    let received = rx.recv().unwrap(); 
    println!("Received: {}", received); 
} 

多生产者单消费者(mpsc)

Rust 标准库提供的 mpsc(Multiple Producers, Single Consumer)通道允许多个发送者向同一个接收者发送消息。 下面是一个多生产者的示例:

rust 复制代码
use std::sync::mpsc; 
use std::thread;
use std::time::Duration; 
fn main() { 
    let (tx, rx) = mpsc::channel(); 
    let tx1 = tx.clone(); // 克隆发送端以创建多个生产者 
    
    // 第一个线程发送消息 
    thread::spawn(move || { 
        tx.send("Message 1").unwrap(); 
        thread::sleep(Duration::from_secs(1)); 
    }); 
    
    // 第二个线程发送消息 
    thread::spawn(move || { 
        tx1.send("Message 2").unwrap();
        thread::sleep(Duration::from_secs(1)); 
    }); 
    
    // 主线程接收消息 
    for received in rx { 
        println!("Got: {}", received); 
        } 
} 

异步通道

在异步编程中,可以使用 tokioasync-std 提供的异步通道。 下面是一个使用 tokio 异步通道的示例:

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

#[tokio::main] 
async fn main() { 
    // 创建一个容量为32的异步通道 
    let (tx, mut rx) = mpsc::channel(32); 
    // 启动一个异步任务发送消息 
    tokio::spawn(async move { 
        tx.send("Hello from async task!").await.unwrap(); 
        println!("Async message sent"); 
    }); 
    
    // 主线程接收消息 
    if let Some(msg) = rx.recv().await { 
        println!("Received: {}", msg); 
    } 
} 

所有权转移

Rust 的消息传递涉及所有权转移,这确保了线程安全。当一个值被发送到通道时,其所有权也随之转移,避免了数据竞争。

rust 复制代码
use std::sync::mpsc; 
use std::thread; 
fn main() { 
    let (tx, rx) = mpsc::channel(); 
    let data = vec![1, 2, 3]; 
    // 转移 data 的所有权到新线程 
    thread::spawn(move || { 
        tx.send(data).unwrap(); 
        // 这里无法再使用 data,因为所有权已转移 
    }); 
    // 主线程接收数据并获得所有权 
    let received_data = rx.recv().unwrap(); 
    println!("Received data: {:?}", received_data); 
} 

总结

Rust 的消息传递机制依靠通道实现线程间通信,通过所有权转移保证内存安全,是构建并发系统的重要基础。我们可以根据需求选择同步通道、异步通道或其他第三方库提供的消息传递方式。

相关推荐
roamingcode1 天前
我是如何 Vibe Coding,将 AI CLI 工具从 Node.js 迁移到 Rust 并成功发布的
人工智能·rust·node.js·github·claude·github copilot
初恋叫萱萱1 天前
构建高性能生成式AI应用:基于Rust Axum与蓝耘DeepSeek-V3.2大模型服务的全栈开发实战
开发语言·人工智能·rust
superman超哥3 天前
Serde 性能优化的终极武器
开发语言·rust·编程语言·rust serde·serde性能优化·rust开发工具
sayang_shao3 天前
Rust多线程编程学习笔记
笔记·学习·rust
鸿乃江边鸟3 天前
Spark Datafusion Comet 向量化Rust Native--读数据
rust·spark·native·arrow
硬汉嵌入式3 天前
基于Rust构建的单片机Ariel RTOS,支持Cortex-M、RISC-V 和 Xtensa
单片机·rust·risc-v
低调滴开发4 天前
Tauri开发桌面端服务,配置指定防火墙端口
rust·tauri·桌面端·windows防火墙规则
咚为4 天前
Rust Cell使用与原理
开发语言·网络·rust
咸甜适中5 天前
rust的docx-rs库,自定义docx模版批量生成docx文档(逐行注释)
开发语言·rust·docx·docx-rs
FAFU_kyp5 天前
RISC0_ZERO项目在macOs上生成链上证明避坑
开发语言·后端·学习·macos·rust