← Back to Rust Course | Chapter 15: Cargo, Testing & Best Practices | Lesson 1 of 8

Cargo.toml को समझना

Cargo.toml आपके project का ID card है, इसका नाम, version, और बाकी सब कुछ जिस पर यह depend करता है list करते हुए।
Syntax
rust
[package]
name = "name"
version = "0.1.0"
edition = "2021"

[dependencies]
crate_name = "1.0"

The Package Section

Cargo.toml का [package] section आपके project का नाम, version, और इसके target किए Rust edition को record करता है, crate की core identity बनाते हुए।

उदाहरण: The Package Section

bash
[package]
name = "my_project"
version = "0.1.0"
edition = "2021"

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

The Dependencies Section

[dependencies] section आपके project को चाहिए external crates list करता है, version requirements के साथ जिन्हें Cargo compatible versions resolve करने के लिए उपयोग करता है।

उदाहरण: The Dependencies Section

bash
[dependencies]
serde = "1.0"
rand = "0.8"

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

Cargo.toml vs Cargo.lock

जबकि आप यह declare करने के लिए Cargo.toml को हाथ से edit करते हैं कि आप किस पर depend करते हैं, Cargo अपने आप Cargo.lock generate और maintain करता है, एक reproducible build के लिए असल में resolve और उपयोग किए गए exact versions record करते हुए।

उदाहरण: Cargo.toml vs Cargo.lock

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "Cargo.toml: what you declare" to the console (with a trailing newline)
    println!("Cargo.toml: what you declare");
    // Print "Cargo.lock: what was actually resolved and locked in" to the console (with a trailing newline)
    println!("Cargo.lock: what was actually resolved and locked in");
}

Rust Editions

edition field Cargo को आपके code को language rules के एक specific set (जैसे 2015, 2018, या 2021) के खिलाफ compile करने देता है, language को evolve होने देते हुए जबकि पुराना code बिना बदले compile होता रहे।

उदाहरण: Rust Editions

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `editions`, set to `["2015", "2018", "2021"]`
    let editions = ["2015", "2018", "2021"];
    // Loop over `editions.iter()`, binding each item to `e`
    for e in editions.iter() {
        // Print "Rust edition: {}" to the console, substituting in `e` (with a trailing newline)
        println!("Rust edition: {}", e);
    }
}
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. [dependencies] section को एक गलत version format के साथ edit करना, जो cargo build को dependencies resolve करने में fail कराता है।
  2. Cargo.toml (manifest जो आप हाथ से edit करते हैं) को Cargo.lock (exact resolved versions, आमतौर पर auto-managed) के साथ confuse करना।
  3. crates.io पर एक updated crate publish करने से पहले version field bump करना भूल जाना।
चैप्टर सारांश
  • Cargo.toml manifest file है जो किसी project के metadata, edition, और dependencies describe करती है।
  • [package] section नाम, version, और edition रखता है; [dependencies] external crates list करता है।
  • Cargo.lock exact resolved dependency versions record करता है और आमतौर पर binaries के लिए commit किया जाता है, Cargo द्वारा auto-generated।
  • Editions (जैसे 2021) Rust को समय के साथ syntax/semantics evolve करने देते हैं बिना पुराने projects को तोड़े।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.