0
0
Rustprogramming~5 mins

Defining modules in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a module in Rust?
A module in Rust is a way to organize code into separate namespaces. It helps group related functions, structs, and other items together, making code easier to manage and understand.
Click to reveal answer
beginner
How do you define a module inside a Rust file?
You define a module using the mod keyword followed by the module name and curly braces containing the module's code. For example:
mod greetings { fn hello() { println!("Hi!"); } }
Click to reveal answer
intermediate
What is the difference between mod and use in Rust modules?
mod defines a new module or declares it exists, while use brings items from a module into scope so you can use them without full paths.
Click to reveal answer
beginner
How do you make a function inside a module accessible from outside the module?
You add the pub keyword before the function to make it public. For example:
pub fn greet() { println!("Hello!"); }
Without pub, the function is private to the module.
Click to reveal answer
intermediate
How can you split a module into multiple files in Rust?
You create a folder with the module name and add a mod.rs file inside it, or in newer Rust versions, create a file named after the module. Then, submodules are files inside that folder. This helps organize large modules.
Click to reveal answer
Which keyword is used to define a module in Rust?
Afn
Buse
Cmod
Dpub
How do you make a function inside a module accessible from other modules?
AUse <code>use</code> before the function
BUse <code>mod</code> before the function
CFunctions are always accessible
DAdd <code>pub</code> before the function
What does the use keyword do in Rust modules?
ADefines a new module
BImports items from a module into scope
CMakes a function public
DDeletes a module
If you define mod greetings {} inside main.rs, where can you use the items inside greetings?
AAnywhere in the crate if items are public
BOnly inside <code>greetings</code>
COnly outside <code>greetings</code>
DNowhere
How can you organize a large module into multiple files?
ABy creating submodules in separate files
BBy putting all code in one file
CBy using <code>use</code> to import files
DRust does not support multiple files for modules
Explain how to define a module in Rust and make a function inside it accessible from outside.
Think about keywords to create modules and control access.
You got /3 concepts.
    Describe how Rust modules help organize code and how you can split modules into multiple files.
    Consider how big projects keep code tidy.
    You got /4 concepts.