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

Tokio का परिचय

Tokio एक popular helper library है जो असल में जानती है कि आपके लिए उन सभी रुके async tasks को कैसे चलाना है।
Syntax
rust
#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        // task
    });
    handle.await.unwrap();
}

Adding Tokio as a Dependency

Tokio उपयोग करना इसे Cargo.toml में एक external dependency के रूप में जोड़ने से शुरू होता है, क्योंकि यह Rust standard library का हिस्सा नहीं है और इसे crates.io से fetch किया जाना चाहिए।

उदाहरण: Adding Tokio as a Dependency

bash
[dependencies]
tokio = { version = "1", features = ["full"] }

⚠️ Run this in your own terminal or Node.js environment.

The #[tokio::main] Attribute

#[tokio::main] macro async fn main() के ऊपर रखा जाता है, इसे एक असली program entry point में बदलते हुए पर्दे के पीछे Tokio का async runtime setup करके।

यह snippet concept रूप से ऐसे code की shape दिखाता है; इसे असल में चलने के लिए tokio crate और network access चाहिए।

उदाहरण: The #[tokio::main] Attribute

markup
// Conceptual shape only -- requires the tokio crate (not available offline):
//
// #[tokio::main]
// async fn main() {
//     let result = say_hello().await;
//     println!("{}", result);
// }
//
// async fn say_hello() -> String {
//     String::from("Hello from an async task")
// }

fn main() {
    println!("This shows the shape of #[tokio::main] code without running it");
    println!("Real Tokio programs need the crate fetched from crates.io");
}

What Tokio Provides

सिर्फ futures चलाने से आगे, Tokio async-aware networking, timers, channels, और task spawning प्रदान करता है, सभी एक cooperative scheduler के तहत efficiently एक साथ काम करने के लिए design किए गए।

उदाहरण: What Tokio Provides

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `features`, set to `["async TCP/UDP networking", "timers and sleep", "task spawning", "channels for message passing"]`
    let features = ["async TCP/UDP networking", "timers and sleep", "task spawning", "channels for message passing"];
    // Loop over `features.iter()`, binding each item to `f`
    for f in features.iter() {
        // Print "Tokio provides: {}" to the console, substituting in `f` (with a trailing newline)
        println!("Tokio provides: {}", f);
    }
}

Why This Example Cannot Run Here

चूंकि इस course के code runner के पास crates.io से crates download करने के लिए network access नहीं, एक असली Tokio-based program को इस environment में compile या execute नहीं किया जा सकता -- यहां सिर्फ plain standard-library Rust चल सकता है।

उदाहरण: Why This Example Cannot Run Here

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "Real async runtimes like Tokio require fetching an external crate" to the console (with a trailing newline)
    println!("Real async runtimes like Tokio require fetching an external crate");
    // Print "Without network access, only std-library-only Rust can compile and run here" to the console (with a trailing newline)
    println!("Without network access, only std-library-only Rust can compile and run here");
}
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. यह मान लेना कि Tokio Rust standard library का हिस्सा है -- यह एक external crate है जिसे Cargo के ज़रिए एक dependency के रूप में जोड़ा जाना चाहिए।
  2. यह भूल जाना कि #[tokio::main] वह है जो एक async fn main() को एक regular fn main() में बदलता है जो runtime setup और चलाता है।
  3. बिना network access के networked या timer-based async examples चलाने की कोशिश करना, जो इस course के code runner जैसे offline sandbox में काम नहीं करेगा।
चैप्टर सारांश
  • Tokio एक widely used asynchronous runtime crate है, Cargo.toml में एक external dependency के रूप में जोड़ा गया।
  • #[tokio::main] attribute macro एक async fn main() को Tokio का executor setup करके एक runnable program में बदलता है।
  • Tokio Future trait के आस-पास बने I/O, timers, और task spawning के async-friendly versions प्रदान करता है।
  • चूंकि Tokio को crates.io से download किया जाना चाहिए, इसे network access चाहिए और यह पूरी तरह offline sandbox में नहीं चल सकता।
🔒

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.