Look at this Rust code. What will it print when run?
fn main() {
println!("Hello, {}!", "world");
}Check how println! replaces {} with the argument.
The println! macro prints the string with {} replaced by the argument "world". So it prints "Hello, world!".
Consider this Rust code where the main function is renamed to start. What will happen when you try to run it?
fn start() {
println!("Starting program");
}The Rust compiler looks for a function named main as the entry point.
Rust requires a main function as the program entry point. Renaming it causes a compilation error because the compiler cannot find main.
This Rust program tries to return an integer from main. Why does it fail to compile?
fn main() {
42
}Check the required signature of the main function in Rust.
In Rust, the main function without an explicit return type implicitly returns the unit type (). The body here evaluates to an i32 (42), causing a type mismatch error.
Choose the best description of the main function's role in Rust.
Think about what happens when you run a Rust program.
The main function is the first code that runs when a Rust program starts. It is the entry point.
Consider this Rust code with two main functions inside modules. What will be printed when you run the program?
mod inner {
pub fn main() {
println!("Inner main");
}
}
fn main() {
println!("Outer main");
inner::main();
}Only the top-level main function is the entry point. Other functions named main inside modules are normal functions.
The program starts at the top-level main function, which prints "Outer main" then calls inner::main() that prints "Inner main".