Rust Modules
Modules
As your programs grow, you will write many functions and structs. Modules let you group related code under one name, like putting files into folders.
You have already used a module. In the HashMap chapter, you wrote this line:
Example
use std::collections::HashMap;
std is Rust's standard library, and collections is a module inside it that contains HashMap.
Create a Module
To create a module, use the mod keyword, followed by a name and curly braces. Put functions inside, and write pub before each function that you want to use outside the module:
Example
mod greetings {
pub fn hello() {
println!("Hello!");
}
pub fn goodbye() {
println!("Goodbye!");
}
}
fn main() {
greetings::hello();
greetings::goodbye();
}
Try it Yourself »
To call a function in a module, write the module name, two colons :: and the function name: greetings::hello().
Private by Default
Everything in a module is private by default. This means that it can only be used inside the module. If you forget pub, you get an error:
Example
mod greetings {
fn hello() {
println!("Hello!");
}
}
fn main() {
greetings::hello(); // Error
}
Try it Yourself »
Private functions are useful for helper code that only the module itself needs.
The use Keyword
Writing the module name every time can make the code long. The use keyword brings a function into scope, so you can call it by its name alone:
Example
mod math {
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn multiply(a: i32, b: i32) -> i32 {
a * b
}
}
use math::add;
fn main() {
println!("{}", add(2, 3));
println!("{}", math::multiply(2, 3));
}
Try it Yourself »
add() can now be called by its name alone. multiply() was not brought in with use, so it still needs math::.
Nested Modules
A module can contain other modules. Write :: between each name in the path:
Example
mod school {
pub mod students {
pub fn list() {
println!("John, Liam");
}
}
pub mod teachers {
pub fn list() {
println!("Jenny");
}
}
}
fn main() {
school::students::list();
school::teachers::list();
}
Try it Yourself »
Both inner modules have a function called list(). The modules keep them apart, so the names do not conflict.
Structs in Modules
Structs can be in modules too. Write pub before the struct, and before each field that should be used outside the module:
Example
mod game {
pub struct Player {
pub name: String,
pub score: i32,
}
impl Player {
pub fn new(name: &str) -> Player {
Player { name: String::from(name), score: 0 }
}
}
}
fn main() {
let mut player = game::Player::new("Jenny");
player.score += 10;
println!("{} has {} points", player.name, player.score);
}
Try it Yourself »
The new() function also needs pub, because it is called from main().
Modules in Separate Files
In bigger projects, each module is usually stored in its own file. For example, a Cargo project (see Get Started) can have a file called src/math.rs next to src/main.rs:
src/math.rs
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
src/main.rs
mod math;
fn main() {
println!("{}", math::add(2, 3));
}
mod math; (with a semicolon instead of curly braces) tells Rust to load the module from the file math.rs. Everything else works the same way.
Note: Our online editor uses only one file, so the examples in this tutorial keep the modules in the same file.
Why Use Modules?
- To group related functions and structs
- To hide helper code that other parts of the program do not need
- To keep the same names in different places without conflicts