文件操作通常包括 I/O 操作。Rust 为我们提供了不同的方法来处理所有这些操作。打开Rust 提供了一个 open 静态方法,我们可以使用它以只读模式打开文件。示例请考虑以下代码:use std::fs::File; use std::io::prelude::*; use std::path::Path; fn main() { // 创建目标文件的路径 let path = Path::new("out.txt"); let display = path.display(); let mut file = match File::open(&path) { Err(why) => panic!("unable to open {}: {}", display, why), Ok(file) => file, }; let mut s = String::new(); match ... 阅读更多
通道是允许两个或多个线程之间进行通信的媒介。Rust 提供了异步通道,使线程之间能够进行通信。Rust 中的通道允许两个端点之间进行单向通信流。这两个端点是发送方和接收方。示例请考虑以下示例:use std::sync::mpsc::{Sender, Receiver}; use std::sync::mpsc; use std::thread; static NTHREADS: i32 = 3; fn main() { let (tx, rx): (Sender, Receiver) = mpsc::channel(); let mut children = Vec::new(); for id in 0..NTHREADS { let thread_tx = tx.clone(); let child = thread::spawn(move || { ... 阅读更多