Crates

Created: 12/01/2025, 08:42 PM

LILLITH FINISH ME

start here

  • can be either a binary or a library
  • a package is a group of one or more crates
  • a package can have as many binary crates as it wants, but max 1 library crate

Binary Crates

  • must have a main function
    • wouldnt you know, it needs to be called main!
      • i never wouldve guessed

Library Crates

  • has a “root” file, usually called lib.rs

Modules

  • defined with mod <moduleName>

  • compiler will search the following paths for the module code:

    • ./garden.rs
    • ./garden/mod.rs
  • you can have submodules, they need to be explicitly defined aswell

    • follows the same syntax as above for defining and searching for the module code
  • by default, can only be accessed by itself. use pub mod to allow any access

  • can be accessed by crate::<moduleName>::<functionName>

    • shortcut via use <moduleName>::<functionName> and access via functionName
  • can be defined inline

    • mod front_of_house {
          mod hosting {
              fn add_to_waitlist() {}
      
              fn seat_at_table() {}
          }
      
          mod serving {
              fn take_order() {}
      
              fn serve_order() {}
      
              fn take_payment() {}
          }
      }`