0
0
Rustprogramming~20 mins

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

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