2022-12-21 16:36:30 +01:00
|
|
|
# Modules
|
|
|
|
|
2023-01-04 15:44:37 +01:00
|
|
|
We have seen how `impl` blocks let us namespace functions to a type.
|
2022-12-21 16:36:30 +01:00
|
|
|
|
|
|
|
Similarly, `mod` lets us namespace types and functions:
|
|
|
|
|
|
|
|
```rust,editable
|
|
|
|
mod foo {
|
|
|
|
pub fn do_something() {
|
|
|
|
println!("In the foo module");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod bar {
|
|
|
|
pub fn do_something() {
|
|
|
|
println!("In the bar module");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo::do_something();
|
|
|
|
bar::do_something();
|
|
|
|
}
|
|
|
|
```
|
2023-02-09 12:50:43 -08:00
|
|
|
|
|
|
|
<details>
|
|
|
|
|
|
|
|
* Packages provide functionality and include a `Cargo.toml` file that describes how to build a bundle of 1+ crates.
|
|
|
|
* Crates are a tree of modules, where a binary crate creates an executable and a library crate compiles to a library.
|
2023-02-16 03:19:25 +00:00
|
|
|
* Modules define organization, scope, and are the focus of this section.
|
2023-02-09 12:50:43 -08:00
|
|
|
|
|
|
|
</details>
|