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

आपका पहला Rust Program

आपका पहला Rust program instructions का एक छोटा set है जो computer को screen पर शब्द print करने को कहता है।
Syntax
rust
fn main() {
    println!("text");
}

The main Function

हर executable Rust program में main नाम का एक function होना चाहिए। यह entry point है: जब आप compiled program चलाते हैं, execution main के अंदर पहली line से शुरू होकर top से bottom तक आगे बढ़ता है।

उदाहरण: The main Function

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

Printing with println!

println! एक macro है, कोई regular function नहीं -- इसलिए यह exclamation mark से खत्म होता है। यह अपना argument standard output पर एक newline character के साथ print करता है, जो इसे Rust program से output देखने का सबसे आम तरीका बनाता है।

Note: println! जैसे Macros variable संख्या में arguments accept कर सकते हैं, जो regular Rust functions नहीं कर सकते।

उदाहरण: Printing with println!

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "This line has a newline after it" to the console (with a trailing newline)
    println!("This line has a newline after it");
    // Print "So this prints on a new line" to the console (with a trailing newline)
    println!("So this prints on a new line");
}

Compiling and Running

आप Rust source code को rustc compiler से एक runnable program में बदलते हैं, जो एक native executable produce करता है। फिर आप उस executable को सीधे command line से चलाते हैं।

उदाहरण: Compiling and Running

bash
rustc main.rs
./main

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

Statements End With Semicolons

किसी Rust function के अंदर ज़्यादातर lines statements हैं, और Rust चाहता है कि हर एक semicolon से खत्म हो। semicolon भूल जाना सबसे आम early compiler errors में से एक है।

उदाहरण: Statements End With Semicolons

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `message`, set to `"Semicolons separate statements"`
    let message = "Semicolons separate statements";
    // Print "{}" to the console, substituting in `message` (with a trailing newline)
    println!("{}", message);
}
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. किसी statement के आखिर में semicolon भूल जाना, जो Rust कुछ scripting languages के विपरीत चाहता है।
  2. file को rustc को पास की गई चीज़ से अलग कुछ नाम देना, फिर यह सोचना कि compiler इसे क्यों नहीं ढूंढ पा रहा।
  3. यह भूल जाना कि main ज़रूरी entry point function नाम है -- Rust हमेशा fn main() से चलना शुरू करता है।
चैप्टर सारांश
  • हर Rust program को एक main function चाहिए, जहां execution शुरू होता है।
  • println! text print करने के लिए उपयोग होने वाला एक macro है (! note करें) जिसके बाद newline आता है।
  • Source files .rs से खत्म होती हैं और rustc filename.rs से compile होती हैं, एक executable produce करते हुए।
  • Rust में statements semicolon से खत्म होते हैं।
🔒

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.