Future Functions
Async utility functions
std::future::ready
Section titled “std::future::ready”use std::future::Future;
async fn use_ready() -> i32 { // ready() creates immediately-ready future let future = std::future::ready(42); future.await}
fn main() { // ready creates a future that's already complete let future = std::future::ready("hello");
println!("=== std::future::ready ==="); println!("Creates Future that immediately returns value"); println!();
// Use case: returning from async fn without await async fn get_cached(use_cache: bool) -> String { if use_cache { std::future::ready("cached".to_string()).await } else { // actual async work "fetched".to_string() } }
// Type signature fn returns_future() -> impl Future<Output = i32> { std::future::ready(42) }
println!("Common uses:"); println!("- Return early from async fn"); println!("- Mock async operations"); println!("- Convert sync to async API");}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google