Concept Flow - main function and entry point
Program starts
Look for main()
Execute main() body
Program ends
Rust programs start by running the main() function, which is the entry point where execution begins and ends.
fn main() {
println!("Hello, world!");
}| Step | Action | Code Line | Output |
|---|---|---|---|
| 1 | Program starts and looks for main() | fn main() { | |
| 2 | Enter main function | fn main() { | |
| 3 | Execute println! macro | println!("Hello, world!"); | Hello, world! |
| 4 | End of main function reached | } | |
| 5 | Program terminates |
| Variable | Start | After main start | After println! | Final |
|---|---|---|---|---|
| No variables | - | - | - | - |
Rust programs start execution at the main() function. main() is the required entry point. Code inside main() runs sequentially. Program ends when main() finishes. Use println! to print output inside main().
main function in a Rust program?mainmain function is special in Rust because it is where the program begins execution.main runs the first instructions when the program starts.fn, followed by the name and parentheses.fn main() {}. Other options use syntax from other languages.fn main() {
println!("Hello, world!");
}println! macro prints the text inside quotes to the screen followed by a newline.println!("Hello, world!"); inside main, so it prints Hello, world! exactly.fn main() {
println!("Welcome to Rust")
}Pass the test and this pattern moves unknown