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

相关推荐
laomocoder2 小时前
AI网关设计
人工智能·rust·系统架构
zandy10113 小时前
业界首发|衡石科技HENGSHI CLI重磅登场,以Rust架构开启Agentic BI自动驾驶时代
科技·架构·rust·agentic bi
王家视频教程图书馆3 小时前
rust 写gui 程序 最流行的是哪个
开发语言·后端·rust
迷藏49412 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
迷藏49420 小时前
**发散创新:基于 Rust的模型保护机制设计与实践**在人工智能快速发
java·人工智能·python·rust·neo4j
love530love20 小时前
从零搭建本地版 Claurst:基于 Rust 重构的 Claude Code 终端编码助手 + LM Studio 模型接入测试
开发语言·人工智能·windows·重构·rust·lm studio·claude code
恋喵大鲤鱼20 小时前
如何理解 Rust 没有运行时(No Runtime)
rust
Tomhex2 天前
Rust数组与Vec的核心差异解析
rust
橘子编程2 天前
编程语言全指南:从C到Rust
java·c语言·开发语言·c++·python·rust·c#
亿牛云爬虫专家2 天前
学术文献爬虫 OOM 崩溃与 403 风暴
爬虫·rust·爬虫代理·403·oom killer·学术文献·403 forbidden