Sample questions
Question 1Easy
What does use std::collections::HashMap; do?
- Downloads the std crate from the registry
- Brings HashMap into scope so it can be named directly
- Imports the whole std library again at run time
- 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
- Paths can be long.
- use shortens them.
- 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?
- pub
- export
- extern
- 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
- Items are private by default.
- pub lets code outside the module use the item.
- 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:
- Public everywhere
- Visible only in the same file
- Private to that module
- 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
- Default is privacy.
- pub opens access.
- 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?
- It re-exports Sq, so code can use it via this module's path
- It copies the whole Sq struct definition into this file as new code
- It makes Sq public only inside the test module and nowhere else
- 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
- use brings a name into scope.
- pub makes that name part of the module's public API.
- 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?
- Visible only to the parent module and its siblings
- Visible to every crate that depends on this one
- Visible only inside the same function body
- 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
- pub widens visibility.
- (crate) restricts it to this crate.
- 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;
}
- It compiles, and the program runs and sets radius to 1.0
- It compiles with a warning only, and the program still runs
- Compile error: the field radius is private
- 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
- Circle is pub, so main can name the type.
- radius is not pub, so it is private to shapes.
- The struct literal in main fails.
- 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());
}
- 5
- 10
- 2
- 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
- super::helper() calls helper in the crate root.
- helper returns 5.
- call multiplies 5 by 2.
- 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());
}
- Yes: pub(super) makes f visible to the whole program everywhere
- Yes: pub(super) is exactly the same as pub, so it compiles
- No: f can only be called from inside module b itself, and no other place
- 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
- f is declared pub(super) inside b.
- super for b is a, so f is visible in a and below.
- main is in the crate root, which is outside a.
- 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 →