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

Cargo Run और Build

cargo build और cargo run सरल commands हैं जो आपके Rust code को एक program में बदल देते हैं और, अगर आप चाहें, इसे तुरंत run कर देते हैं।
Syntax
rust
cargo run [--release] [-- args]
cargo build [--release]

Building a Project

cargo build आपके project को बिना run किए compile करता है। Resulting debug executable target/debug/ में रखी जाती है, और default रूप से यह build debug information include करती है और heavy optimizations skip करती है ताकि compilation खुद तेज़ हो।

उदाहरण: Building a Project

bash
cargo build

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

Running a Project

cargo run एक convenience command है जो आपके project को compile करता है (सिर्फ अगर source files बदली हों) और फिर तुरंत resulting binary execute करता है। यह वह command है जिसे आप development के दौरान सबसे ज़्यादा उपयोग करेंगे।

उदाहरण: Running a Project

bash
cargo run

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

Release Builds

--release पास करना Cargo को compiler optimizations apply करने को कहता है, लंबे compile times की कीमत पर एक छोटी और कहीं ज़्यादा तेज़ binary produce करते हुए। Release builds वही हैं जिन्हें आप production में ship करते हैं या performance benchmarking के लिए उपयोग करते हैं।

उदाहरण: Release Builds

bash
cargo build --release

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

Seeing the Output in Code

चाहे debug में build हो या release mode में, program का logic और output identical हैं -- सिर्फ speed और binary size अलग होते हैं। यह उदाहरण बिल्कुल वही है जो cargo run एक project scaffold करने के बाद execute करता।

उदाहरण: Seeing the Output in Code

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "Built with cargo build, or run directly with cargo run" to the console (with a trailing newline)
    println!("Built with cargo build, or run directly with cargo run");
}
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. quick iteration के लिए सीधे cargo run उपयोग करने के बजाय cargo build चलाना और फिर हाथ से binary ढूंढना।
  2. यह न समझना कि cargo build --release एक कहीं ज़्यादा तेज़, optimized binary produce करता है, और इसके बजाय debug build के साथ benchmarking करना।
  3. target/ folder को यह सोचकर delete करना कि इसमें source code है, जबकि यह safe-to-delete build output है।
चैप्टर सारांश
  • cargo build project compile करता है और executable को target/debug/ के नीचे रखता है।
  • cargo run compile करता है (अगर ज़रूरत हो) और एक ही step में तुरंत resulting binary चलाता है।
  • cargo build --release एक optimized binary produce करता है target/release/ के नीचे, जो production/performance के लिए उपयोग होता है।
  • Cargo सिर्फ बदली हुई files को फिर से compile करता है, जो repeated builds को तेज़ बनाता है।
🔒

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.