0
0
Rustprogramming~20 mins

Module visibility in Rust - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rust Module Visibility Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Rust code with module visibility?

Consider the following Rust code. What will it print when run?

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

fn main() {
    outer::inner::greet();
}
ANo output
Berror: function `greet` is private
CHello from inner module!
Derror: module `inner` is private
Attempts:
2 left
💡 Hint

Check which modules and functions are marked pub to be accessible outside.

Predict Output
intermediate
2:00remaining
What error does this Rust code produce due to module visibility?

What error will this Rust code produce when compiled?

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

fn main() {
    outer::inner::greet();
}
Aerror: module `inner` is private
BHello from inner module!
Cerror: function `greet` is private
Derror: function `greet` not found in `outer`
Attempts:
2 left
💡 Hint

Remember that modules are private by default unless marked pub.

🔧 Debug
advanced
2:00remaining
Why does this Rust code fail to compile due to visibility?

Given this Rust code, why does it fail to compile?

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

fn main() {
    outer::inner::greet();
}
AModule `inner` is private inside `outer` module
BFunction `greet` is private inside `inner` module
CFunction `greet` is not declared
DModule `outer` is private
Attempts:
2 left
💡 Hint

Check the visibility of the function greet inside the public module.

📝 Syntax
advanced
2:00remaining
Which option correctly makes a nested module and function public in Rust?

Choose the option that correctly declares a public nested module inner and a public function greet inside it.

Apub mod outer { pub mod inner { fn greet() {} } }
Bpub mod outer { mod inner { pub fn greet() {} } }
Cmod outer { mod inner { fn greet() {} } }
Dmod outer { pub mod inner { pub fn greet() {} } }
Attempts:
2 left
💡 Hint

Both the nested module and the function must be marked pub to be accessible outside.

🚀 Application
expert
3:00remaining
How many items are accessible from outside in this Rust module structure?

Given this Rust code, how many functions can be called from main without compilation errors?

Rust
mod outer {
    pub mod inner1 {
        pub fn func1() {}
        fn func2() {}
    }
    mod inner2 {
        pub fn func3() {}
        pub fn func4() {}
    }
    pub fn func5() {}
}

fn main() {
    outer::inner1::func1();
    // outer::inner1::func2(); // commented
    // outer::inner2::func3(); // commented
    // outer::inner2::func4(); // commented
    outer::func5();
}
A2
B3
C4
D5
Attempts:
2 left
💡 Hint

Count only functions that are pub and inside pub modules accessible from main.