Bird
Raised Fist0
Rustprogramming~10 mins

Program structure in Rust - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. In a Rust program, where does the execution start?
easy
A. At the last line of the file
B. Inside the main function
C. Inside a function named start
D. At the first line of the file outside any function

Solution

  1. Step 1: Understand Rust program entry point

    Rust programs always begin execution from the main function.
  2. Step 2: Check other options

    Code outside functions or other function names like start are not entry points.
  3. Final Answer:

    Inside the main function -> Option B
  4. Quick Check:

    Rust starts at main() [OK]
Hint: Rust always runs code starting from main() [OK]
Common Mistakes:
  • Thinking code outside main() runs first
  • Assuming a function named start() runs automatically
  • Believing execution starts at the last line
2. Which of the following is the correct way to write the main function in Rust?
easy
A. function main() { println!("Hello, world!"); }
B. main() { println!("Hello, world!"); }
C. fn main() { println!("Hello, world!"); }
D. def main() { println!("Hello, world!"); }

Solution

  1. Step 1: Identify Rust function syntax

    Rust functions start with fn, followed by the name and parentheses.
  2. Step 2: Check each option

    Only fn main() { println!("Hello, world!"); } uses fn main() correctly; others use wrong keywords.
  3. Final Answer:

    fn main() { println!("Hello, world!"); } -> Option C
  4. Quick Check:

    Rust functions start with fn [OK]
Hint: Rust functions start with fn keyword [OK]
Common Mistakes:
  • Using function or def instead of fn
  • Omitting fn keyword
  • Missing parentheses after function name
3. What will be the output of this Rust program?
fn main() {
    println!("Start");
    {
        println!("Inside block");
    }
    println!("End");
}
medium
A. Start\nInside block\nEnd
B. Start\nEnd\nInside block
C. Inside block\nStart\nEnd
D. Start\nInside block

Solution

  1. Step 1: Follow the code execution order

    The program prints "Start", then enters the block and prints "Inside block", then prints "End" after the block.
  2. Step 2: Confirm output sequence

    The output lines appear in the order: Start, Inside block, End.
  3. Final Answer:

    Start\nInside block\nEnd -> Option A
  4. Quick Check:

    Code runs top to bottom inside blocks [OK]
Hint: Code runs step by step inside braces [OK]
Common Mistakes:
  • Assuming block runs after all code
  • Mixing order of print statements
  • Ignoring braces as code grouping
4. Identify the error in this Rust program:
fn main() {
    println!("Hello");
    // Missing closing brace
medium
A. println! macro is misspelled
B. Missing semicolon after println!
C. Incorrect comment syntax
D. Missing closing brace '}' for main function

Solution

  1. Step 1: Check code structure

    The main function starts with fn main() { but never closes with a }.
  2. Step 2: Verify other syntax

    The semicolon after println! is present, comment syntax is correct, and println! is spelled correctly.
  3. Final Answer:

    Missing closing brace '}' for main function -> Option D
  4. Quick Check:

    Every { needs a matching } [OK]
Hint: Count opening and closing braces carefully [OK]
Common Mistakes:
  • Forgetting to close function with }
  • Confusing comment syntax with errors
  • Missing semicolon errors instead of braces
5. You want to add a comment explaining why your Rust program prints "Hello". Which is the correct way to add a single-line comment inside the main function?
hard
A. // This prints Hello println!("Hello");
B. # This prints Hello println!("Hello");
C. /* This prints Hello */ println!("Hello");
D. println!("Hello");

Solution

  1. Step 1: Identify Rust comment syntax

    Rust uses // for single-line comments and /* */ for block comments.
  2. Step 2: Check each option

    Only // This prints Hello println!("Hello"); uses Rust's single-line comment syntax correctly; /* ... */ is a block comment, # is Python-style, and <!-- ... --> is HTML.
  3. Final Answer:

    // This prints Hello println!("Hello"); -> Option A
  4. Quick Check:

    Single-line comments start with // [OK]
Hint: Use // for single-line comments in Rust [OK]
Common Mistakes:
  • Using # or for comments
  • Confusing block comments with single-line
  • Placing comments inside println! macro
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard