Medium10 minRust Fundamentals
UpdatedAug 4, 2026
Edit

Modules and Visibility

CONCEPTS:Rust Modules and Visibility

Question Variations

  • "What is the difference between a crate and a module?"
  • "Does `use` make an item public?"
  • "When would you prefer `pub(crate)` over `pub`?"
  • "How does Rust's default privacy affect library design?"

Why This Is Asked

Module boundaries shape a Rust crate’s maintainability and API safety. This question tests whether you can organize code around clear responsibilities and understand that privacy is the default rather than an afterthought.

Key Concepts

  • A crate is a compilation unit; modules organize names within it.
  • Items are private by default to their parent module.
  • pub exposes an item, while pub(crate) and pub(super) provide narrower visibility.
  • use brings a path into scope but does not change visibility.

Question Variations

  • “What is the difference between a crate and a module?”
  • “Does use make an item public?”
  • “When would you prefer pub(crate) over pub?”
  • “How does Rust’s default privacy affect library design?”

Answers by Technology

+ Add Variant
RustImprove this answer ✏️

Expected Answer (Rust 1.97.1)

A crate is a compilation unit, such as a binary or library. Modules create a hierarchy of names inside a crate and provide privacy boundaries. Items are private to their parent module by default, so a public API must be intentionally exposed with pub or a narrower visibility modifier such as pub(crate).

use creates a convenient local name for a path; it does not itself grant access or make an item public.

mod billing {
    pub struct Invoice {
        id: u64,
    }

    impl Invoice {
        pub fn new(id: u64) -> Self {
            Self { id }
        }

        pub fn id(&self) -> u64 {
            self.id
        }
    }
}

fn main() {
    let invoice = billing::Invoice::new(42);
    assert_eq!(invoice.id(), 42);
}

External code can construct Invoice only through its public constructor; its field remains encapsulated.

Why It Matters

Intentional visibility keeps a crate’s public contract small, enables internal refactoring, and prevents consumers from depending on implementation details. It also makes ownership and invariants easier to protect.

Common Mistakes

  • Making fields public to avoid writing methods: This exposes representation details and makes future changes harder.
  • Assuming use changes access control: It only imports a name into the current scope.
  • Using pub for every internal helper: Prefer the narrowest visibility that satisfies the required callers.

Follow-up Questions

  • What does pub(crate) mean? (Answer: The item is visible anywhere within the current crate but not to external crates.)
  • How can a child module access a private parent item? (Answer: Privacy is defined relative to parent modules, so a child can access items visible to its parent.)