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

println! Macro का उपयोग

println! वह command है जिसे आप अपने Rust program को screen पर text दिखाने के लिए उपयोग करते हैं।
Syntax
rust
println!("text");
println!("value: {}", expr);
println!("{name}: {:?}", debug_value);

Basic Printing

println! का सबसे simple उपयोग एक plain string literal लेता है और इसे newline के साथ print करता है। Rust सीखते समय ज़्यादातर लोग सबसे पहले यही लिखते हैं।

उदाहरण: Basic Printing

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

Placeholders With {}

Curly braces {} format string के अंदर placeholders की तरह काम करते हैं, जो string के बाद पास किए गए extra arguments से order में भरे जाते हैं। यह आपको एक call में literal text को dynamic values के साथ mix करने देता है।

Note: Ferris Rust का unofficial mascot है -- एक friendly crab।

उदाहरण: Placeholders With {}

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `name`, set to `"Ferris"`
    let name = "Ferris";
    // Declare an immutable variable `age`, set to `5`
    let age = 5;
    // Print "{} is {} years old" to the console, substituting in `name, age` (with a trailing newline)
    println!("{} is {} years old", name, age);
}

Named Arguments

आप braces के अंदर सीधे placeholders को नाम दे सकते हैं, जैसे {value}, और Rust इन्हें format string के बाद पास किए named arguments से match करता है, उनकी position चाहे जो भी हो। यह लंबे format strings को पढ़ने में आसान बना सकता है।

उदाहरण: Named Arguments

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `city`, set to `"Berlin"`
    let city = "Berlin";
    // Declare an immutable variable `temp`, set to `21`
    let temp = 21;
    // Print "It is {} degrees in {}" to the console, substituting in `temp, city` (with a trailing newline)
    println!("It is {} degrees in {}", temp, city);
}

print! vs println!

print! बिल्कुल println! जैसा behave करता है सिवाय इसके कि यह trailing newline नहीं जोड़ता। इसका मतलब है consecutive print! calls एक ही line पर जारी रहते हैं, जो piece by piece output बनाने के लिए उपयोगी है।

उदाहरण: print! vs println!

markup
// Entry point: execution of the program starts here
fn main() {
    // Print "No newline here... " to the console (without a newline)
    print!("No newline here... ");
    // Print "but this one ends the line" to the console (with a trailing newline)
    println!("but this one ends the line");
}
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. println!("{}", x, y) उपयोग करना ज़्यादा placeholders या arguments के साथ जो असल में match नहीं करते, एक compile error का कारण बनते हुए।
  2. यह भूल जाना कि curly braces {} placeholders के रूप में ज़रूरी हैं और पुराने-style %s/%d formatting से replace नहीं हो सकतीं।
  3. print! (कोई trailing newline नहीं) को println! (एक trailing newline जोड़ता है) के साथ confuse करना और squished-together output पाना।
चैप्टर सारांश
  • println! text को newline के साथ print करता है; print! बिना किसी के print करता है।
  • Curly braces {} placeholders हैं जो बाद में आने वाले arguments से order में भरे जाते हैं।
  • Braces के अंदर named या positional arguments उपयोग किए जा सकते हैं, जैसे {name} या {0}।
  • format! print किए बिना एक formatted String बनाता है, वही syntax उपयोग करते हुए जो println! उपयोग करता है।
🔒

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.