Skip to content
เข้าสู่ระบบ

Async Patterns

Patterns สำหรับ async/await programming

// async fn returns impl Future
async fn fetch_data() -> String {
// Simulated async work
"data from server".to_string()
}
async fn process_data(data: String) -> usize {
data.len()
}
async fn main_workflow() -> usize {
// .await suspends until complete
let data = fetch_data().await;
let result = process_data(data).await;
result
}
fn main() {
// Block on async in sync context
// In real code, use tokio::main or async-std::main
println!("Async patterns demonstration");
// Futures are lazy - nothing runs until polled
let future = fetch_data(); // Not executed yet
println!("Future created");
// Would need runtime to execute:
// let result = tokio::runtime::Runtime::new()
// .unwrap()
// .block_on(main_workflow());
}

เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม

ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด