Bird
Raised Fist0
Rustprogramming~20 mins

Compilation and execution flow in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Compilation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust program?

Consider this Rust code snippet. What will it print when run?

Rust
fn main() {
    let x = 5;
    let y = { let x = 3; x + 1 };
    println!("{}", x + y);
}
A8
B6
C9
DCompile error
Attempts:
2 left
๐Ÿ’ก Hint

Remember that inner blocks can shadow outer variables temporarily.

โ“ Predict Output
intermediate
2:00remaining
What error does this Rust code produce?

What error will this Rust code cause when compiled?

Rust
fn main() {
    let mut x = 10;
    let y = &x;
    x = 20;
    println!("{}", y);
}
APrints 20
BCompile error: cannot assign to `x` because it is borrowed
CPrints 10
DRuntime panic: borrow error
Attempts:
2 left
๐Ÿ’ก Hint

Rust enforces borrowing rules at compile time.

๐Ÿง  Conceptual
advanced
2:00remaining
Order of compilation steps in Rust

Which of the following correctly lists the main steps Rust compiler performs in order?

A1, 2, 3, 4
B2, 1, 3, 4
C1, 3, 2, 4
D3, 1, 2, 4
Attempts:
2 left
๐Ÿ’ก Hint

Think about how source code is processed from raw text to machine code.

๐Ÿ”ง Debug
advanced
2:00remaining
Why does this Rust program panic at runtime?

Examine this Rust code. Why does it panic when run?

Rust
fn main() {
    let v = vec![1, 2, 3];
    let x = v[3];
    println!("{}", x);
}
APrints 0
BCompile error: index out of bounds
CPrints 3
DIndex out of bounds panic at runtime
Attempts:
2 left
๐Ÿ’ก Hint

Check the vector length and the index used.

๐Ÿš€ Application
expert
3:00remaining
What is the output of this Rust async program?

Consider this Rust async code using tokio. What will it print?

Rust
#[tokio::main]
async fn main() {
    let handle = tokio::spawn(async {
        5 + 3
    });
    let result = handle.await.unwrap();
    println!("{}", result);
}
A8
BCompile error: missing runtime
CRuntime panic: unwrap on None
D0
Attempts:
2 left
๐Ÿ’ก Hint

Think about what tokio::spawn returns and how await works.

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