0
0
Rustprogramming~20 mins

Program structure in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Program Structure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output 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");
}
AHello, Bob!\nHello, Alice!
BHello, Alice! Hello, Bob!
CHello, Alice!\nHello, Alice!
DHello, Alice!\nHello, Bob!
Attempts:
2 left
๐Ÿ’ก Hint
Look at how the greet function is called twice with different names.
๐Ÿง  Conceptual
intermediate
1:30remaining
Understanding Rust's main function and program entry
Which statement about the Rust program entry point is true?
ARust programs start from the first function defined in the file.
BThe program starts execution from the main function.
CRust requires a function named start as the entry point.
DThe entry point is determined by the order of function calls.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what function the Rust compiler looks for to begin running the program.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify 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");
}
Aerror: multiple definitions of `main` function
Berror: function main must return a value
Cerror: missing semicolon after println!
Derror: println! macro not found
Attempts:
2 left
๐Ÿ’ก Hint
Check if Rust allows more than one main function in the same program.
๐Ÿ“ Syntax
advanced
2:30remaining
Correct 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?
A
mod math {
    fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

fn main() {
    let sum = math::add(2, 3);
    println!("{}", sum);
}
B
module math {
    fn add(a: i32, b: i32) -> i32 {
        return a + b;
    }
}

fn main() {
    let sum = math.add(2, 3);
    println!("{}", sum);
}
C
mod math {
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

fn main() {
    let sum = math::add(2, 3);
    println!("{}", sum);
}
D
mod math {
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

fn main() {
    let sum = add(2, 3);
    println!("{}", sum);
}
Attempts:
2 left
๐Ÿ’ก Hint
Remember that functions inside modules must be public to be accessed outside, and the module syntax is `mod`.
๐Ÿš€ Application
expert
2:00remaining
Predict 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());
}
A3
B5
C2
D4
Attempts:
2 left
๐Ÿ’ก Hint
Count how many numbers from 0 to 4 are even.