Modules and Visibility
Module Path
Calling a Public Function
A module groups related items. pub makes a function callable from outside the module, and module::item names the path.
Program
Play the program to choose a multiplication factor and call a public function through its module path.
module_path.rs
mod math {
pub fn scale(value: i32, factor: i32) -> i32 {
value * factor
}
}
fn main() {
let factor = ;
let result = math::scale(5, factor);
println!("{result}");
}
mod math {
pub fn scale(value: i32, factor: i32) -> i32 {
value * factor
}
}
fn main() {
let factor = ;
let result = math::scale(5, factor);
println!("{result}");
}
mod math {
pub fn scale(value: i32, factor: i32) -> i32 {
value * factor
}
}
fn main() {
let factor = ;
let result = math::scale(5, factor);
println!("{result}");
}
mod
`mod math` defines a nested module.
pub fn
`pub fn scale` can be called from outside `math`.
path
`math::scale` names the function through the module path.