0
0
Rustprogramming~10 mins

Program structure in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Program structure in Rust
Start
fn main()
Statements inside main
Program ends
Rust programs start with the main function, which runs statements inside it, then ends.
Execution Sample
Rust
fn main() {
    println!("Hello, world!");
}
This program prints 'Hello, world!' to the screen.
Execution Table
StepActionCode LineOutput
1Program startsfn main() {
2Enter main function println!("Hello, world!");
3Print to screen println!("Hello, world!");Hello, world!
4Exit main function}
5Program ends
💡 Program ends after main function finishes.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
No variables----
Key Moments - 3 Insights
Why does Rust need a main function?
Rust programs always start running from the main function, as shown in step 2 of the execution table.
What does println! do inside main?
println! prints text to the screen, which you can see in step 3 where 'Hello, world!' is output.
Can code run outside main in Rust?
No, Rust runs code inside functions like main; the program ends after main finishes (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
AThe program prints 'Hello, world!'
BThe program starts
CThe program ends
DThe program enters main
💡 Hint
Check the Output column at step 3 in the execution table.
At which step does the program exit the main function?
AStep 2
BStep 5
CStep 4
DStep 3
💡 Hint
Look at the Action column for 'Exit main function' in the execution table.
If we add another println! after the first, how would the output change?
AIt would print nothing
BIt would print two lines of text
CIt would cause an error
DIt would print only the first line
💡 Hint
Think about how println! works inside main as shown in the execution table.
Concept Snapshot
Rust programs start with fn main() {
Inside main, statements run in order
Use println! to print text
Program ends after main finishes
No code runs outside functions
Full Transcript
This visual trace shows how a Rust program runs starting from the main function. The program begins at step 1, enters main at step 2, executes the println! statement at step 3 which prints 'Hello, world!' to the screen, then exits main at step 4 and ends at step 5. Variables are not used here. Key points include that Rust always starts running from main, println! prints text, and the program ends after main finishes. The quiz questions check understanding of these steps and outputs.