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. }
  6. mod bar {
  7. pub fn do_something() {
  8. println!("In the bar module");
  9. }
  10. }
  11. fn main() {
  12. foo::do_something();
  13. bar::do_something();
  14. }
  • 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.