आपका पहला Rust Program
In this page:
fn main() {
println!("text");
}
The main Function
हर executable Rust program में main नाम का एक function होना चाहिए। यह entry point है: जब आप compiled program चलाते हैं, execution main के अंदर पहली line से शुरू होकर top से bottom तक आगे बढ़ता है।
उदाहरण: The main Function
// Entry point: execution of the program starts here
fn main() {
// Print "Hello, Rust!" to the console (with a trailing newline)
println!("Hello, Rust!");
}
Login to try C/C++/Java/PHP code in the editor
Printing with println!
println! एक macro है, कोई regular function नहीं -- इसलिए यह exclamation mark से खत्म होता है। यह अपना argument standard output पर एक newline character के साथ print करता है, जो इसे Rust program से output देखने का सबसे आम तरीका बनाता है।
उदाहरण: Printing with println!
// 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");
}
Login to try C/C++/Java/PHP code in the editor
Compiling and Running
आप Rust source code को rustc compiler से एक runnable program में बदलते हैं, जो एक native executable produce करता है। फिर आप उस executable को सीधे command line से चलाते हैं।
उदाहरण: Compiling and Running
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
// 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);
}
Login to try C/C++/Java/PHP code in the editor
- किसी statement के आखिर में semicolon भूल जाना, जो Rust कुछ scripting languages के विपरीत चाहता है।
- file को
rustcको पास की गई चीज़ से अलग कुछ नाम देना, फिर यह सोचना कि compiler इसे क्यों नहीं ढूंढ पा रहा। - यह भूल जाना कि
mainज़रूरी entry point function नाम है -- Rust हमेशाfn main()से चलना शुरू करता है।
- हर Rust program को एक
mainfunction चाहिए, जहां 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: