Bird
Raised Fist0
Rustprogramming~20 mins

Defining modules in Rust - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What keyword do you use in Rust to define a new module inside a file?
easy
A. module
B. use
C. mod
D. pub

Solution

  1. Step 1: Understand Rust module declaration

    Rust uses the keyword mod to declare a module inside a file.
  2. Step 2: Differentiate from other keywords

    use imports modules, pub makes items public, and module is not a Rust keyword.
  3. Final Answer:

    mod -> Option C
  4. Quick Check:

    Keyword for defining modules = mod [OK]
Hint: Remember: 'mod' means module in Rust [OK]
Common Mistakes:
  • Using 'module' instead of 'mod'
  • Confusing 'use' with 'mod'
  • Forgetting 'mod' keyword
2. Which of the following is the correct syntax to declare a module named network in Rust?
easy
A. use network {}
B. module network {}
C. pub mod network() {}
D. mod network {}

Solution

  1. Step 1: Recall correct module syntax

    The correct syntax to declare a module is mod name { } without parentheses.
  2. Step 2: Check each option

    mod network {} uses mod network {} which is correct. module network {} uses 'module' which is invalid. pub mod network() {} adds parentheses which are not allowed. use network {} uses 'use' which is for importing.
  3. Final Answer:

    mod network {} -> Option D
  4. Quick Check:

    Module declaration = mod name {} [OK]
Hint: Modules use curly braces, no parentheses [OK]
Common Mistakes:
  • Adding parentheses after module name
  • Using 'module' instead of 'mod'
  • Confusing 'use' with 'mod'
3. What will be the output of this Rust code?
mod math {
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }
}

fn main() {
    println!("{}", math::add(3, 4));
}
medium
A. 7
B. Compilation error
C. math::add(3, 4)
D. 0

Solution

  1. Step 1: Check module and function visibility

    The function add is marked pub, so it can be accessed outside the module.
  2. Step 2: Evaluate the function call

    Calling math::add(3, 4) returns 3 + 4 = 7, which is printed.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    Public function call returns sum = 7 [OK]
Hint: Public functions in modules can be called with module::function [OK]
Common Mistakes:
  • Forgetting 'pub' causes compilation error
  • Expecting function name printed
  • Confusing output with zero
4. Identify the error in this Rust code:
mod utils {
    fn helper() {
        println!("Helper function");
    }
}

fn main() {
    utils::helper();
}
medium
A. Function 'helper' is private and cannot be accessed outside the module
B. Missing semicolon after println!
C. Module 'utils' is not declared correctly
D. Cannot call functions inside modules

Solution

  1. Step 1: Check function visibility

    The function helper is not marked pub, so it is private to the module.
  2. Step 2: Understand access from main

    Trying to call utils::helper() from outside causes a compilation error due to privacy.
  3. Final Answer:

    Function 'helper' is private and cannot be accessed outside the module -> Option A
  4. Quick Check:

    Private functions can't be called outside module [OK]
Hint: Add 'pub' to functions to access them outside modules [OK]
Common Mistakes:
  • Assuming all functions are public by default
  • Ignoring privacy rules
  • Thinking modules block all calls
5. You want to organize your Rust project by splitting code into two files: main.rs and network.rs. How do you correctly declare and use the network module in main.rs?
hard
A. Write pub mod network {} in main.rs and put code inside network.rs
B. Write mod network; in main.rs and make functions in network.rs public with pub
C. Write use network; in main.rs and declare mod network {} inside network.rs
D. Write extern crate network; in main.rs and import functions manually

Solution

  1. Step 1: Understand module file linking

    To link an external file as a module, use mod network; in main.rs. This tells Rust to load network.rs.
  2. Step 2: Make functions public in network.rs

    Functions inside network.rs must be marked pub to be accessible from main.rs.
  3. Final Answer:

    Write mod network; in main.rs and make functions in network.rs public with pub -> Option B
  4. Quick Check:

    Use mod filename; and pub functions for multi-file modules [OK]
Hint: Use 'mod filename;' in main and 'pub' for access [OK]
Common Mistakes:
  • Using 'use' instead of 'mod' to declare modules
  • Declaring mod inside the module file
  • Using extern crate for local modules
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard