tezvyn:

Explain the difference between mod and use in Rust

AI-drafted, machine-checkedSource: doc.rust-lang.orgintermediate
WHAT IT TESTS

declaration versus import in Rust's module system.

ANSWER OUTLINE

mod tells the compiler to compile a file into the crate tree; use brings an existing path into scope as a shortcut.

WHAT THIS TESTS: This question probes whether you understand the two distinct jobs in Rust's module system. mod is about declaration and compilation structure: it tells rustc to include a new module in the crate tree and where to find its source. use is about scope convenience: it creates a local shortcut to an item that is already part of the crate tree. Seniors should show they know mod changes the build graph while use only affects name resolution in the current scope.

A GOOD ANSWER COVERS: First, state clearly that mod declares a module and adds it to the crate. The compiler searches for the code in specific places such as src/garden.rs or src/garden/mod.rs when it sees mod garden. Second, explain that use never declares anything new; it simply brings a path like crate::garden::vegetables::Asparagus into the current namespace so you do not have to type the full path repeatedly. Third, mention privacy: mod controls whether a submodule is public via pub mod, while use respects existing visibility boundaries. Fourth, give a concrete file layout showing the crate root declaring modules and a leaf file using use to access deeply nested items.

COMMON WRONG ANSWERS: Calling both keywords imports is the biggest red flag. Another mistake is saying mod pulls in dependencies from crates.io; that is Cargo.toml's job. Some candidates also think use is required to access another module's items; you can always use the full path without use. Finally, forgetting that mod is needed exactly once per module in the crate tree, usually in the parent module or crate root, shows shallow understanding.

LIKELY FOLLOW-UPS: The interviewer might ask what happens if you write mod garden twice, or where the compiler looks when you declare mod vegetables inside src/garden.rs. They may ask about the difference between pub mod and mod, or when to use the older src/garden/mod.rs style versus src/garden.rs. You might also be asked how use super::something works compared to use crate::something.

ONE CONCRETE EXAMPLE: Imagine a binary crate named backyard with src/main.rs, src/garden.rs, and src/garden/vegetables.rs. In src/main.rs you write pub mod garden; to declare the garden module and tell the compiler to compile src/garden.rs. Inside src/garden.rs you write pub mod vegetables; to include src/garden/vegetables.rs. That file contains pub struct Asparagus. Back in src/main.rs, instead of writing let plant = crate::garden::vegetables::Asparagus;, you can write use crate::garden::vegetables::Asparagus; and then let plant = Asparagus;. The mod lines build the tree; the use line is purely for ergonomic access.

Read the original → doc.rust-lang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.