Cargo के Basics
In this page:
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
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
[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
// 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");
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- Cargo को project scaffold करने देने के बजाय किसी single-file experiment से आगे किसी चीज़ के लिए हाथ से Rust files लिखना।
target/directory के अंदर files edit करना, जो regenerated build output है और overwrite हो जाती है।- यह भूल जाना कि project metadata और dependencies
Cargo.tomlमें रहते हैं, source files में बिखरे नहीं।
- Cargo Rust का official build tool और package manager है, जो rustup के ज़रिए rustc के साथ अपने आप installed होता है।
cargo new project_nameएकCargo.tomlfile और एकsrc/main.rsstarter file के साथ एक नया project scaffold करता है।Cargo.tomlproject 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: