Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions content/blog/async/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+++
title = "The State of Async Rust: Runtimes"
date = 2024-02-21
updated = 2025-03-28
updated = 2026-05-09
draft = false
template = "article.html"
[extra]
Expand Down Expand Up @@ -404,10 +404,8 @@ channel:
```rust
use std::error::Error;
use std::fs;
use std::io::Read;
use std::path::Path;
use std::{thread, time};
use std::sync::mpsc;
use std::thread;

fn main() {
let (tx, rx) = mpsc::channel::<String>();
Expand All @@ -416,21 +414,26 @@ fn main() {

thread::scope(|scope| {
for file in files {
scope.spawn(move || {
let contents = fs::read_to_string(file);
// ...
let tx = tx.clone();
scope.spawn(move || -> Result<(), Box<dyn Error + Send + Sync>> {
let contents = fs::read_to_string(file)?;
tx.send(contents)?;
Ok(())
});
}
});

// Drop the original sender so the `rx` loop knows when to stop
drop(tx);

// Receive messages from the channel
for received in rx {
println!("Got: {:?}", received);
}
}
```

([Link to playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5b7fa587cf1796089ce7bb374539229e))
([Link to playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6f134711236a897f8d5b53cd5798fe37))


## Common Prejudice Against Threads
Expand Down
Loading