Challenge - 5 Problems
Rust Program Structure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput of a simple Rust program with functions
What is the output of this Rust program?
Rust
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet("Alice");
greet("Bob");
}Attempts:
2 left
๐ก Hint
Look at how the greet function is called twice with different names.
โ Incorrect
The greet function prints a greeting with the given name. It is called first with "Alice" then with "Bob", so the output prints two lines, each greeting the respective name.
๐ง Conceptual
intermediateUnderstanding Rust's main function and program entry
Which statement about the Rust program entry point is true?
Attempts:
2 left
๐ก Hint
Think about what function the Rust compiler looks for to begin running the program.
โ Incorrect
In Rust, the main function is the designated entry point where execution begins. Other functions are called from main or indirectly.
๐ง Debug
advancedIdentify the error in this Rust program structure
What error does this Rust code produce when compiled?
Rust
fn main() {
println!("Start");
}
fn main() {
println!("Duplicate main");
}Attempts:
2 left
๐ก Hint
Check if Rust allows more than one main function in the same program.
โ Incorrect
Rust programs cannot have more than one main function. Defining main twice causes a compilation error about multiple definitions.
๐ Syntax
advancedCorrect syntax for defining a module and using it
Which option correctly defines a module named `math` with a function `add` and calls it from main?
Attempts:
2 left
๐ก Hint
Remember that functions inside modules must be public to be accessed outside, and the module syntax is `mod`.
โ Incorrect
Option C correctly defines a public function inside a module and calls it with the module path. Option C uses invalid syntax `module` and dot notation. Option C's function is private, so calling it outside causes error. Option C calls add without module prefix, which is invalid.
๐ Application
expertPredict the number of items in a vector after conditional push
Consider this Rust code. How many elements does the vector `numbers` contain after execution?
Rust
fn main() {
let mut numbers = Vec::new();
for i in 0..5 {
if i % 2 == 0 {
numbers.push(i);
}
}
println!("Count: {}", numbers.len());
}Attempts:
2 left
๐ก Hint
Count how many numbers from 0 to 4 are even.
โ Incorrect
The loop runs from 0 to 4. It pushes only even numbers (0, 2, 4) into the vector. So the vector has 3 elements.
