Modules

We have seen how impl blocks let us namespace functions to a type.

Similarly, mod lets us namespace types and functions:

1. mod foo {
2. pub fn do_something() {
3. println!("In the foo module");
4. }
5. }

7. mod bar {
8. pub fn do_something() {
9. println!("In the bar module");
10. }
11. }

13. fn main() {
14. foo::do_something();
15. bar::do_something();
16. }
  • 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.
  • Modules define organization, scope, and are the focus of this section.