println! Macro का उपयोग
println! वह command है जिसे आप अपने Rust program को screen पर text दिखाने के लिए उपयोग करते हैं।In this page:
println!("text");
println!("value: {}", expr);
println!("{name}: {:?}", debug_value);
Basic Printing
println! का सबसे simple उपयोग एक plain string literal लेता है और इसे newline के साथ print करता है। Rust सीखते समय ज़्यादातर लोग सबसे पहले यही लिखते हैं।
उदाहरण: Basic Printing
// 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");
}
Login to try C/C++/Java/PHP code in the editor
Placeholders With {}
Curly braces {} format string के अंदर placeholders की तरह काम करते हैं, जो string के बाद पास किए गए extra arguments से order में भरे जाते हैं। यह आपको एक call में literal text को dynamic values के साथ mix करने देता है।
उदाहरण: Placeholders With {}
// 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);
}
Login to try C/C++/Java/PHP code in the editor
Named Arguments
आप braces के अंदर सीधे placeholders को नाम दे सकते हैं, जैसे {value}, और Rust इन्हें format string के बाद पास किए named arguments से match करता है, उनकी position चाहे जो भी हो। यह लंबे format strings को पढ़ने में आसान बना सकता है।
उदाहरण: Named Arguments
// 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);
}
Login to try C/C++/Java/PHP code in the editor
print! vs println!
print! बिल्कुल println! जैसा behave करता है सिवाय इसके कि यह trailing newline नहीं जोड़ता। इसका मतलब है consecutive print! calls एक ही line पर जारी रहते हैं, जो piece by piece output बनाने के लिए उपयोगी है।
उदाहरण: print! vs println!
// 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");
}
Login to try C/C++/Java/PHP code in the editor
println!("{}", x, y)उपयोग करना ज़्यादा placeholders या arguments के साथ जो असल में match नहीं करते, एक compile error का कारण बनते हुए।- यह भूल जाना कि curly braces
{}placeholders के रूप में ज़रूरी हैं और पुराने-style%s/%dformatting से replace नहीं हो सकतीं। 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 किए बिना एक formattedStringबनाता है, वही syntax उपयोग करते हुए जोprintln!उपयोग करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: