← Back to Rust Course | Chapter 1: Setup & Basics | Lesson 4 of 7

Cargo के Basics

Cargo एक helper है जो आपके लिए नए Rust projects बनाता है और उनके सभी pieces को organized रखता है।
Syntax
rust
cargo new project_name
cargo build
cargo run
cargo check

Creating a New Project

cargo new एक पूरा, ready-to-build Rust project scaffold करता है: यह एक project folder, एक Cargo.toml manifest file, एक src/ directory, और एक starter src/main.rs file बनाता है जिसमें एक 'Hello, world!' program होता है।

उदाहरण: Creating a New Project

bash
cargo new hello_cargo

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

The Cargo.toml Manifest

Cargo.toml project की manifest file है। यह package name, version, Rust edition, और project को चाहिए किसी भी external dependencies (crates) को record करता है। Cargo यह जानने के लिए इस file को पढ़ता है कि आपका project कैसे build करना है।

उदाहरण: The Cargo.toml Manifest

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

[dependencies]

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

The src Directory

Cargo आपके source code को एक src/ folder के अंदर उम्मीद करता है, src/main.rs एक binary program के लिए entry point होने के साथ। इस structure को रखने का मतलब है हर Cargo command बिना extra configuration के बिल्कुल जानता है कि आपका code कहां ढूंढना है।

उदाहरण: The src Directory

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "Generated by cargo new" to the console (with a trailing newline)
    println!("Generated by cargo new");
}

Why Use Cargo Instead of rustc Directly

जैसे ही कोई project एक file से बड़ा हो या external libraries चाहिए, सीधे rustc call करना tedious हो जाता है।

Cargo मुट्ठी भर सरल subcommands से compiling, dependency management, testing, और packaging को automate करता है, यही कारण है कि लगभग सभी असली Rust projects इसे उपयोग करते हैं।

उदाहरण: Why Use Cargo Instead of rustc Directly

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `reasons`, set to `["dependency management", "consistent builds", "built-in testing"]`
    let reasons = ["dependency management", "consistent builds", "built-in testing"];
    // Loop over `reasons.iter()`, binding each item to `r`
    for r in reasons.iter() {
        // Print "Cargo handles: {}" to the console, substituting in `r` (with a trailing newline)
        println!("Cargo handles: {}", r);
    }
}
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. Cargo को project scaffold करने देने के बजाय किसी single-file experiment से आगे किसी चीज़ के लिए हाथ से Rust files लिखना।
  2. target/ directory के अंदर files edit करना, जो regenerated build output है और overwrite हो जाती है।
  3. यह भूल जाना कि project metadata और dependencies Cargo.toml में रहते हैं, source files में बिखरे नहीं।
चैप्टर सारांश
  • Cargo Rust का official build tool और package manager है, जो rustup के ज़रिए rustc के साथ अपने आप installed होता है।
  • cargo new project_name एक Cargo.toml file और एक src/main.rs starter file के साथ एक नया project scaffold करता है।
  • Cargo.toml project metadata (name, version) और dependencies store करता है।
  • target/ directory compiled build artifacts रखती है और इसे हाथ से edit नहीं करना चाहिए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

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