Modules & Visibility Questions with Answers

8 practice questions on Modules & Visibility (Rust) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

What does use std::collections::HashMap; do?

  1. Downloads the std crate from the registry
  2. Brings HashMap into scope so it can be named directly
  3. Imports the whole std library again at run time
  4. Creates a new empty HashMap value

Answer: B. Brings HashMap into scope so it can be named directly

Explanation

use creates a shorter path to an item so you can write HashMap instead of the full path.

Step-by-step solution

  1. Paths can be long.
  2. use shortens them.
  3. No data is created.

Why the other options are wrong

  • A. std ships with the toolchain.
  • C. Imports are resolved at compile time.
  • D. It only creates a name binding.

Question 2Easy

Which keyword makes a function or struct visible outside its module?

  1. pub
  2. export
  3. extern
  4. open

Answer: A. pub

Explanation

Items are private to their module by default. Adding pub lets code outside the module use the item.

Step-by-step solution

  1. Items are private by default.
  2. pub lets code outside the module use the item.
  3. Without pub, other modules cannot name it.

Why the other options are wrong

  • B. Rust does not use export; visibility is set with pub.
  • C. extern is for linking to code written in other languages, not for visibility.
  • D. Rust has no open keyword for visibility.

Tip: Make things pub only when other modules need them.

Question 3Easy

By default, items inside a Rust module are:

  1. Public everywhere
  2. Visible only in the same file
  3. Private to that module
  4. Visible to child crates only

Answer: C. Private to that module

Explanation

Everything is private unless marked pub. Child modules can see their ancestors' private items, not vice versa.

Step-by-step solution

  1. Default is privacy.
  2. pub opens access.
  3. Hence private.

Why the other options are wrong

  • A. You must write pub to expose them.
  • B. Visibility follows modules, not files.
  • D. There are no child crates.

Question 4Medium

What does pub use crate::geo::shapes::Sq; do inside a module?

  1. It re-exports Sq, so code can use it via this module's path
  2. It copies the whole Sq struct definition into this file as new code
  3. It makes Sq public only inside the test module and nowhere else
  4. It downloads the geo crate from crates.io and adds it to Cargo.toml

Answer: A. It re-exports Sq, so code can use it via this module's path

Explanation

pub use creates a public alias for an item. Other code can then use the short path through this module, which is how libraries flatten their API.

Step-by-step solution

  1. use brings a name into scope.
  2. pub makes that name part of the module's public API.
  3. So outside code can refer to the item through this module.

Why the other options are wrong

  • B. use never copies code; it creates a name for an item that already exists.
  • C. pub use is not limited to tests; it re-exports the item for anyone who can reach the module.
  • D. use paths refer to modules in your own crate or in std, not to downloads.

Tip: Re-exports let you hide an awkward internal module layout behind a clean public path.

Question 5Medium

What does pub(crate) mean?

  1. Visible only to the parent module and its siblings
  2. Visible to every crate that depends on this one
  3. Visible only inside the same function body
  4. Visible anywhere inside the current crate but not outside it

Answer: D. Visible anywhere inside the current crate but not outside it

Explanation

pub(crate) limits access to the crate, handy for internals shared across modules.

Step-by-step solution

  1. pub widens visibility.
  2. (crate) restricts it to this crate.
  3. Outside crates cannot use it.

Why the other options are wrong

  • A. That is pub(super).
  • B. That is plain pub.
  • C. Functions don't define visibility scopes this way.

Question 6Medium

What happens when this code is compiled?

mod shapes {
    pub struct Circle { radius: f64 }
    impl Circle {
        pub fn new(r: f64) -> Self { Circle { radius: r } }
    }
}

fn main() {
    let c = shapes::Circle { radius: 1.0 };
    let _ = c;
}
  1. It compiles, and the program runs and sets radius to 1.0
  2. It compiles with a warning only, and the program still runs
  3. Compile error: the field radius is private
  4. It panics at run time when main builds the Circle value

Answer: C. Compile error: the field radius is private

Explanation

Struct fields are private by default. main is outside the shapes module, so it cannot set radius with a struct literal. Circle::new is the public way to build one.

Step-by-step solution

  1. Circle is pub, so main can name the type.
  2. radius is not pub, so it is private to shapes.
  3. The struct literal in main fails.
  4. Call shapes::Circle::new(1.0) instead.

Why the other options are wrong

  • A. The field is private to shapes, so main cannot set it directly.
  • B. Using a private field from outside its module is an error, not a warning.
  • D. The problem is caught at compile time, so the program never runs.

Tip: Expose a pub fn new constructor when fields should stay private.

Question 7Medium

What is printed?

fn helper() -> i32 { 5 }

mod inner {
    pub fn call() -> i32 { super::helper() * 2 }
}

fn main() {
    println!("{}", inner::call());
}
  1. 5
  2. 10
  3. 2
  4. Compile error

Answer: B. 10

Explanation

super refers to the parent module. inner is a child of the crate root, so it can call the private helper. helper returns 5, and call doubles it to 10.

Step-by-step solution

  1. super::helper() calls helper in the crate root.
  2. helper returns 5.
  3. call multiplies 5 by 2.
  4. println prints 10.

Why the other options are wrong

  • A. 5 is what helper() returns, but call() doubles it.
  • C. 2 would be 1 * 2, which the code never computes.
  • D. A child module can use private items of its parent, so super::helper() compiles.

Tip: Child modules can see private items in their ancestors; siblings and outside code cannot.

Question 8Hard

Does this code compile?

mod a {
    pub mod b {
        pub(super) fn f() -> i32 { 1 }
    }
}

fn main() {
    println!("{}", a::b::f());
}
  1. Yes: pub(super) makes f visible to the whole program everywhere
  2. Yes: pub(super) is exactly the same as pub, so it compiles
  3. No: f can only be called from inside module b itself, and no other place
  4. No: f is visible only in a and its children, and main is outside a

Answer: D. No: f is visible only in a and its children, and main is outside a

Explanation

pub(super) makes an item visible in the parent module and the parent's descendants. Here the parent is a, so f is visible in a, but main in the crate root is outside a and the call is rejected.

Step-by-step solution

  1. f is declared pub(super) inside b.
  2. super for b is a, so f is visible in a and below.
  3. main is in the crate root, which is outside a.
  4. So the call a::b::f() is rejected.

Why the other options are wrong

  • A. pub(super) is narrower than pub; it is not visible to everyone.
  • B. pub(super) limits visibility to the parent module and below.
  • C. The parent module a can call it too; the problem is that main is outside a.

Tip: pub(super) means visible one level up; pub(crate) means visible across the whole crate.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics