Bird
Raised Fist0
Rustprogramming~10 mins

Compilation and execution flow in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Compilation and execution flow
Write Rust source code
↓
rustc compiler reads code
↓
Syntax and semantic checks
↓
Generate executable
↓
Run executable
↓
Program output on screen
This flow shows how Rust code is written, compiled into an executable, and then run to produce output.
Execution Sample
Rust
fn main() {
    println!("Hello, Rust!");
}
This simple Rust program prints 'Hello, Rust!' to the screen.
Execution Table
StepActionEvaluationResult
1Write source codeCode with fn main and println!Source file created
2Run rustc compilerCheck syntax and semanticsNo errors found
3rustc generates executableTranslate code to machine instructionsExecutable file created
4Run executableStart program executionProgram runs
5Execute main functionCall println! macroPrints 'Hello, Rust!'
6Program endsNo more instructionsProcess exits
7Output displayedCaptured stdoutHello, Rust!
💡 Program finishes after main function completes and output is shown.
Variable Tracker
VariableStartAfter CompilationAfter ExecutionFinal
source_codeemptyRust code textRust code textRust code text
executablenonecreatedrunningfinished
outputnonenoneHello, Rust!Hello, Rust!
Key Moments - 3 Insights
Why do we need to compile Rust code before running it?
Rust is a compiled language, so the source code must be translated into machine code by rustc before the computer can run it. This is shown in steps 2 and 3 of the execution table.
What happens if there is a syntax error in the code?
The compiler finds errors during the syntax and semantic checks (step 2). It stops and shows error messages instead of creating an executable, so the program cannot run.
When does the program actually produce output?
Output is produced during execution of the main function (step 5), when the println! macro runs and prints text to the screen.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the executable file created?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Action' and 'Result' columns in the execution table for when the executable is generated.
According to the variable tracker, what is the state of 'output' after compilation but before execution?
ARust code text
BHello, Rust!
Cnone
Dcreated
💡 Hint
Look at the 'output' row in variable_tracker under 'After Compilation' column.
If a syntax error occurs, which step in the execution table would NOT happen?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Errors stop the process before executable creation, so step 3 is skipped.
Concept Snapshot
Rust compilation flow:
1. Write source code (fn main)
2. Compile with rustc (syntax check + machine code)
3. Create executable file
4. Run executable (main runs)
5. Output printed by println!
6. Program ends
Full Transcript
This visual execution shows how Rust code is written, compiled, and run. First, you write the source code with a main function. Then, the rustc compiler checks the code for errors. If no errors are found, it creates an executable file. Running this executable starts the program, which executes the main function. Inside main, the println! macro prints text to the screen. After printing, the program finishes and exits. Variables like source_code, executable, and output change state during these steps. If there is a syntax error, the compiler stops and shows errors before creating an executable. This flow helps beginners understand the step-by-step process from code to output in Rust.

Practice

(1/5)
1. What is the first step when running a Rust program?
easy
A. Directly run the Rust source file without compiling
B. Compile the code using rustc to create an executable
C. Write the output to a file before compiling
D. Use an interpreter to execute the Rust code line by line

Solution

  1. Step 1: Understand Rust's execution model

    Rust is a compiled language, so the source code must be compiled first.
  2. Step 2: Identify the compilation tool

    The rustc compiler converts Rust code into an executable file.
  3. Final Answer:

    Compile the code using rustc to create an executable -> Option B
  4. Quick Check:

    Rust code is compiled first [OK]
Hint: Rust code must be compiled before running [OK]
Common Mistakes:
  • Thinking Rust runs like a script without compiling
  • Confusing Rust with interpreted languages
  • Trying to run source code directly
2. Which command correctly compiles a Rust file named main.rs?
easy
A. run rustc main.rs
B. compile main.rs
C. rustc main.rs
D. cargo run main.rs

Solution

  1. Step 1: Recall Rust compilation command

    The Rust compiler command is rustc followed by the filename.
  2. Step 2: Check each option

    rustc main.rs matches the correct syntax: rustc main.rs.
  3. Final Answer:

    rustc main.rs -> Option C
  4. Quick Check:

    Use rustc filename.rs to compile [OK]
Hint: Use 'rustc' followed by filename to compile [OK]
Common Mistakes:
  • Adding extra words like 'run' before rustc
  • Using 'compile' instead of 'rustc'
  • Confusing cargo commands with rustc
3. What will be the output when running this Rust program?
fn main() {
    println!("Hello, Rust!");
}
medium
A. Hello, Rust!
B. println!("Hello, Rust!");
C. Error: missing semicolon
D. No output

Solution

  1. Step 1: Understand the println! macro

    This macro prints the string inside quotes to the console with a newline.
  2. Step 2: Check the code correctness

    The code is syntactically correct and will print the message.
  3. Final Answer:

    Hello, Rust! -> Option A
  4. Quick Check:

    println! outputs the string [OK]
Hint: println! prints text to console with newline [OK]
Common Mistakes:
  • Thinking println! prints the code itself
  • Expecting no output without return statement
  • Confusing macro syntax with function
4. Identify the error in this Rust code snippet:
fn main() {
    println!("Hello, world!")
}
medium
A. Missing closing parenthesis for println! macro
B. No error, code runs fine
C. Incorrect function name
D. Missing semicolon after println! macro

Solution

  1. Step 1: Check statement termination syntax

    Rust requires semicolons to terminate expression statements like println! macro calls.
  2. Step 2: Locate the missing semicolon

    The println! call is missing the terminating semicolon before the closing brace.
  3. Final Answer:

    Missing semicolon after println! macro -> Option D
  4. Quick Check:

    Expression statements require semicolon [OK]
Hint: Rust expression statements require a trailing semicolon [OK]
Common Mistakes:
  • Thinking it's a parenthesis mismatch
  • Confusing macros with block expressions
  • Believing no semicolon needed before closing brace
5. You have two Rust files: lib.rs and main.rs. You want to compile main.rs which uses functions from lib.rs. What is the correct compilation and execution flow?
hard
A. Compile lib.rs into a library, then compile main.rs linking the library, then run the executable
B. Run main.rs directly without compiling lib.rs
C. Compile only main.rs, Rust automatically compiles lib.rs
D. Combine both files manually into one file before compiling

Solution

  1. Step 1: Understand Rust's multi-file compilation

    The rustc compiler automatically handles referenced modules like lib.rs when compiling main.rs.
  2. Step 2: Single command suffices

    With mod lib; in main.rs, rustc main.rs compiles both files and produces the executable.
  3. Final Answer:

    Compile lib.rs into a library, then compile main.rs linking the library, then run the executable -> Option A
  4. Quick Check:

    rustc main.rs compiles dependencies automatically [OK]
Hint: rustc compiles referenced modules automatically [OK]
Common Mistakes:
  • Trying to run source files without compiling dependencies
  • Assuming Rust compiles dependencies automatically at runtime
  • Manually merging files instead of compiling separately
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