← Back to Rust Course | Chapter 14: Closures, Iterators & Async | Lesson 5 of 6

Async/Await के Basics

async और .await एक program को किसी धीमी चीज़ का इंतज़ार करते हुए एक task रोकने देते हैं, और इस बीच दूसरे काम के साथ आगे बढ़ने देते हैं।
Syntax
rust
async fn name() -> ReturnType {
    let value = other().await;
    value
}

Declaring an Async Function

एक async fn एक normal function जैसा दिखता है लेकिन एक Future return करता है जो इसका eventual result represent करता है, call होने पर तुरंत इसकी body चलाने के बजाय।

Async syntax को actually execute करने के लिए 2018+ Rust edition और एक runtime चाहिए, इसलिए यह snippet shape को एक comment के रूप में दिखाता है और underlying idea (एक value जो तुरंत तैयार नहीं है) को plain std code से demonstrate करता है।

उदाहरण: Declaring an Async Function

markup
// Async syntax (needs edition 2018+ and a runtime), shown conceptually:
// async fn compute() -> i32 {
//     42
// }

fn main() {
    println!("async fn compute() would return a Future, not an i32 directly");
    println!("It must be awaited inside an async runtime to actually run");
}

The Role of .await

किसी async context के अंदर, .await एक Future पर उपयोग होता है इसे completion तक drive करने के लिए, उस task को रोकते हुए (पूरे program को block किए बिना) जब तक value तैयार न हो।

चूंकि .await को edition 2018+ और एक runtime चाहिए, यह snippet async shape को एक comment के रूप में रखता है और इसके बजाय plain function calls से conceptual chaining दिखाता है।

उदाहरण: The Role of .await

markup
// Conceptual async chain (needs edition 2018+ and a runtime):
// async fn get_value() -> i32 { 10 }
// async fn use_value() -> i32 {
//     let v = get_value().await;
//     v + 1
// }

fn get_value() -> i32 {
    10
}

fn use_value() -> i32 {
    let v = get_value();
    v + 1
}

fn main() {
    println!("Synchronous stand-in result: {}", use_value());
    println!("An async version would use .await instead of a direct call");
}

Why Async Needs a Runtime

Rust standard library Future trait और async/.await syntax define करती है, लेकिन futures को असल में चलाने के लिए एक executor शामिल नहीं करती -- वह काम Tokio या async-std जैसे एक runtime crate से belong करता है।

उदाहरण: Why Async Needs a Runtime

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "The standard library defines Future and async/.await syntax" to the console (with a trailing newline)
    println!("The standard library defines Future and async/.await syntax");
    // Print "But executing futures requires an external runtime crate" to the console (with a trailing newline)
    println!("But executing futures requires an external runtime crate");
    // Print "Common choices: tokio, async-std" to the console (with a trailing newline)
    println!("Common choices: tokio, async-std");
}

Async Is for Concurrency, Not Parallelism

Async/await तब चमकता है जब कोई program एक साथ कई धीमे I/O operations (जैसे network requests) का इंतज़ार कर रहा हो, एक single thread को उन्हें efficiently juggle करने देते हुए, raw CPU-bound computation तेज़ करने के बजाय।

उदाहरण: Async Is for Concurrency, Not Parallelism

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "Async excels at juggling many waiting I/O tasks concurrently" to the console (with a trailing newline)
    println!("Async excels at juggling many waiting I/O tasks concurrently");
    // Print "For CPU-bound parallel work, OS threads are usually the better tool" to the console (with a trailing newline)
    println!("For CPU-bound parallel work, OS threads are usually the better tool");
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि कोई async fn call होते ही तुरंत चलता है -- यह असल में एक lazy Future return करता है जो तब तक कुछ नहीं करता जब तक इसे await या poll न किया जाए।
  2. Futures को असल में completion तक drive करने वाले एक async runtime (जैसे Tokio या async-std) के बिना async code चलाने की कोशिश करना।
  3. यह भूल जाना कि .await सिर्फ किसी async fn या async block के अंदर उपयोग किया जा सकता है, ordinary synchronous code में नहीं।
चैप्टर सारांश
  • एक async fn तुरंत एक Future return करता है, उस काम को represent करते हुए जो अभी शुरू नहीं हुआ।
  • .await एक Future को completion की ओर drive करता है, इसके इंतज़ार के दौरान control runtime को वापस देते हुए।
  • Async code को futures को असल में execute करने के लिए एक runtime (जैसे Tokio) चाहिए -- अकेली standard library उन्हें नहीं चलाती।
  • Async/await कई I/O-bound tasks पर efficiently इंतज़ार करने के लिए design किया गया है, CPU-bound parallelism के लिए नहीं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.