Implement async programming with Tokio runtime
✓Works with OpenClaudeYou are a Rust async programming expert. The user wants to implement async programming with Tokio runtime, including spawning tasks, handling multiple concurrent operations, and managing async/await patterns.
What to check first
- Verify Tokio is in
Cargo.toml:cargo tree | grep tokio - Check Rust version supports async/await (1.39+):
rustc --version - Confirm you have the
tokiofeature flags needed:fullor specific features likert-multi-thread,macros,io-util
Steps
- Add
tokiowith required features toCargo.toml:tokio = { version = "1.0", features = ["full"] } - Use
#[tokio::main]macro on your main function to initialize the runtime automatically - Mark functions as
async fnthat need to perform non-blocking I/O or await other futures - Use
.awaitto wait for futures to complete without blocking the thread - Spawn independent tasks with
tokio::spawn()to run futures concurrently on the runtime - Use
tokio::select!macro to wait for multiple futures and handle the first one to complete - Handle errors with
Resulttypes and the?operator in async contexts - Join spawned tasks with
JoinHandle::awaitor usetokio::join!for multiple tasks
Code
use std::time::Duration;
use tokio::time::sleep;
use tokio::sync::mpsc;
#[tokio::main]
async fn main() {
println!("Starting async runtime");
// Example 1: Simple async function
fetch_data().await;
// Example 2: Spawn multiple concurrent tasks
let task1 = tokio::spawn(async {
sleep(Duration::from_secs(1)).await;
"Task 1 done"
});
let task2 = tokio::spawn(async {
sleep(Duration::from_millis(500)).await;
"Task 2 done"
});
// Wait for both tasks
let result1 = task1.await.expect("Task 1 panicked");
let result2 = task2.await.expect("Task 2 panicked");
println!("{}, {}", result1, result2);
// Example 3: Channel communication between tasks
let (tx, mut rx) = mpsc::channel(10);
tokio::spawn(async move {
for i in 0..3 {
tx.send(i).await.ok();
sleep(Duration::from_millis(100)).await;
}
});
while let Some(value) = rx.recv().await {
println!("Received: {}", value);
}
// Example 4: Select between multiple futures
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Rust Skills
Other Claude Code skills in the same category — free to download.
Rust CLI
Build fast CLI applications with Clap in Rust
Rust API
Scaffold Rust web API with Actix-web or Axum
Rust Testing
Set up Rust unit and integration testing
Rust WASM
Build WebAssembly modules with Rust and wasm-pack
Rust Error Handling
Implement proper error handling with thiserror and anyhow
Rust Serde
Serialize and deserialize data with Serde in Rust
Rust Error Handling Patterns
Build idiomatic error types using thiserror for libraries and anyhow for applications
Rust Async with Tokio
Write async Rust services with Tokio runtime for high-performance I/O
Want a Rust skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.