0
0
Rustprogramming~20 mins

Why modules are used in Rust - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Modules in Rust

Why do Rust programmers use modules in their code?

ATo make the program run faster by compiling modules separately
BTo allow multiple main functions in the same project
CTo automatically generate documentation without comments
DTo organize code into separate namespaces and control visibility
Attempts:
2 left
💡 Hint

Think about how you keep your desk tidy by putting things in drawers.

Predict Output
intermediate
2:00remaining
Output of Module Visibility Example

What is the output of this Rust code?

Rust
mod kitchen {
    pub fn cook() {
        println!("Cooking food");
    }
    fn clean() {
        println!("Cleaning kitchen");
    }
}

fn main() {
    kitchen::cook();
    // kitchen::clean(); // Uncommenting this line causes error
}
ACleaning kitchen
BCooking food
CCooking food\nCleaning kitchen
DCompilation error due to private function call
Attempts:
2 left
💡 Hint

Only functions marked pub can be called from outside the module.

🔧 Debug
advanced
2:00remaining
Fix the Module Import Error

Why does this Rust code cause a compilation error?

Rust
mod garden {
    pub fn plant() {
        println!("Planting seeds");
    }
}

fn main() {
    plant();
}
ACannot call functions from modules in main
BFunction plant is private; make it public
CFunction plant is not in scope; call it as garden::plant()
DMissing semicolon after mod garden declaration
Attempts:
2 left
💡 Hint

Think about how to access functions inside modules from outside.

📝 Syntax
advanced
2:00remaining
Identify the Syntax Error in Module Declaration

Which option contains the correct syntax to declare a module named tools with a public function hammer?

Amod tools { pub fn hammer() { println!("Hammering"); } }
Bmodule tools { pub fn hammer() { println!("Hammering"); } }
Cmod tools pub fn hammer() { println!("Hammering"); }
Dmod tools { fn hammer() pub { println!("Hammering"); } }
Attempts:
2 left
💡 Hint

Remember the keyword to declare a module and how to mark functions public.

🚀 Application
expert
3:00remaining
How Many Items Are Accessible?

Given this Rust code, how many items can be accessed from main?

Rust
mod library {
    pub mod books {
        pub fn read() {}
        fn secret() {}
    }
    fn helper() {}
    pub fn open() {}
}

fn main() {
    // Which of these can be called here?
    // library::open();
    // library::helper();
    // library::books::read();
    // library::books::secret();
}
A2
B3
C4
D1
Attempts:
2 left
💡 Hint

Count only public items accessible from main.