0
0
Rustprogramming~20 mins

Defining modules in Rust - Practice Problems & Coding Challenges

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!
Predict Output
intermediate
2:00remaining
Output of nested module function call

What is the output of this Rust program?

Rust
mod outer {
    pub mod inner {
        pub fn greet() {
            println!("Hello from inner module!");
        }
    }
}

fn main() {
    outer::inner::greet();
}
AHello from inner module!
BCompilation error: function not found
CRuntime error: module not found
DNo output
Attempts:
2 left
💡 Hint

Check how the function is called using the full path with ::.

Predict Output
intermediate
2:00remaining
Accessing private module item

What error does this Rust code produce?

Rust
mod my_mod {
    fn secret() {
        println!("Secret function");
    }
}

fn main() {
    my_mod::secret();
}
ANo error, prints 'Secret function'
Berror[E0425]: cannot find function `secret` in `my_mod`
Cerror[E0433]: failed to resolve: use of undeclared crate or module `my_mod`
Derror[E0603]: function `secret` is private
Attempts:
2 left
💡 Hint

Functions are private by default inside modules unless marked pub.

🔧 Debug
advanced
2:00remaining
Fix the module import error

Given this Rust code, which option fixes the compilation error?

mod utils {
    pub fn helper() {
        println!("Helping");
    }
}

fn main() {
    helper();
}
AAdd <code>use crate::utils::helper;</code> before <code>fn main()</code>
BChange <code>helper();</code> to <code>utils::helper();</code>
CMake <code>helper</code> function private by removing <code>pub</code>
DAdd <code>mod helper;</code> inside <code>utils</code> module
Attempts:
2 left
💡 Hint

Functions inside modules need to be called with their full path or imported.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in module declaration

Which option contains the syntax error in defining a module?

Rust
mod math {
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}
Amod math pub fn add(a: i32, b: i32) -> i32 { a + b }
Bmod math { pub fn add(a: i32, b: i32) -> i32 { return a + b; } }
Cmod math { pub fn add(a: i32, b: i32) -> i32 { a + b } }
D} } b + a { 23i >- )23i :b ,23i :a(dda nf bup { htam dom
Attempts:
2 left
💡 Hint

Check the syntax for module declaration braces.

🚀 Application
expert
3:00remaining
Number of items in a nested module

Consider this Rust code:

mod outer {
    pub mod inner {
        pub fn f1() {}
        pub fn f2() {}
        fn f3() {}
    }
    fn f4() {}
}

fn main() {
    let count = count_pub_functions();
    println!("{}", count);
}

fn count_pub_functions() -> usize {
    // Imagine this function counts all public functions inside outer and its submodules
    0 // placeholder
}

What should count_pub_functions() return for the number of public functions inside outer and its nested modules?

A4
B3
C2
D1
Attempts:
2 left
💡 Hint

Count all pub fn inside outer and its nested modules.