0
0
Rustprogramming~20 mins

main function and entry point in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Main Function 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?

Look at this Rust code. What will it print when run?

Rust
fn main() {
    println!("Hello, {}!", "world");
}
AHello, world!
BHello, {}!
Cworld
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint

Check how println! replaces {} with the argument.

โ“ Predict Output
intermediate
2:00remaining
What happens if you rename the main function?

Consider this Rust code where the main function is renamed to start. What will happen when you try to run it?

Rust
fn start() {
    println!("Starting program");
}
AProgram runs but prints nothing
BProgram prints "Starting program"
CCompilation error: cannot find function `main`
DRuntime error: function not found
Attempts:
2 left
๐Ÿ’ก Hint

The Rust compiler looks for a function named main as the entry point.

๐Ÿ”ง Debug
advanced
2:00remaining
Why does this Rust program not compile?

This Rust program tries to return an integer from main. Why does it fail to compile?

Rust
fn main() {
    42
}
ABecause <code>main</code> cannot have a return type
BBecause <code>main</code> must return () (unit), not i32
CBecause <code>main</code> must return a String
DBecause <code>main</code> must be async
Attempts:
2 left
๐Ÿ’ก Hint

Check the required signature of the main function in Rust.

๐Ÿง  Conceptual
advanced
2:00remaining
What is the role of the main function in a Rust program?

Choose the best description of the main function's role in Rust.

AIt is a special function that runs after all other functions
BIt automatically runs in parallel with other functions
CIt is used only for testing and is optional
DIt is the program's entry point where execution starts
Attempts:
2 left
๐Ÿ’ก Hint

Think about what happens when you run a Rust program.

โ“ Predict Output
expert
3:00remaining
What is the output of this Rust program with nested main functions?

Consider this Rust code with two main functions inside modules. What will be printed when you run the program?

Rust
mod inner {
    pub fn main() {
        println!("Inner main");
    }
}

fn main() {
    println!("Outer main");
    inner::main();
}
AOuter main\nInner main
BCompilation error: multiple main functions
CInner main\nOuter main
DRuntime error: ambiguous main function
Attempts:
2 left
๐Ÿ’ก Hint

Only the top-level main function is the entry point. Other functions named main inside modules are normal functions.