What is the output of this Rust program?
mod outer {
pub mod inner {
pub fn greet() {
println!("Hello from inner module!");
}
}
}
fn main() {
outer::inner::greet();
}Check how the function is called using the full path with ::.
The function greet is public inside the nested module inner, which is inside outer. Calling outer::inner::greet() prints the message.
What error does this Rust code produce?
mod my_mod {
fn secret() {
println!("Secret function");
}
}
fn main() {
my_mod::secret();
}Functions are private by default inside modules unless marked pub.
The function secret is private because it lacks pub. Trying to call it from outside the module causes an access error.
Given this Rust code, which option fixes the compilation error?
mod utils {
pub fn helper() {
println!("Helping");
}
}
fn main() {
helper();
}Functions inside modules need to be called with their full path or imported.
Calling helper() directly fails because it's inside utils. Using utils::helper() calls it correctly.
Which option contains the syntax error in defining a module?
mod math {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
}Check the syntax for module declaration braces.
Option A misses the braces { } after mod math, causing a syntax error.
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?
Count all pub fn inside outer and its nested modules.
Inside outer, f4 is private. Inside inner, f1 and f2 are public, f3 is private. So total public functions are 2 in inner + 0 in outer = 2.
But the question asks for all public functions inside outer and nested modules, so total is 2.