Notes on Rust Programming Language

2025-01-05

Rust Programming Language

7 - Managing Growing Projects with Packages, Crates, and Modules

  • encapsulation
    • lets you reuse code at a higher level
    • once you've implemented an operation, other code can call your code via its public interface without having to know how the implementation works
  • scope
    • the nested context in which code is written has a set of names that are defined as "in scope"
  • rust features to manage code organization
    • Packages
      • a cargo feature that lets you build, test, and share craftes
    • Crates
      • a tree of modules that produces a library or executable
    • Modules and use
      • let you control the organization, scope, and privacy of paths
    • Paths
      • a way of naming an item, such as a struct, function, or module

Packages and Crates

  • crate
    • smallest amount of code that the Rust compiler considers at a time
  • binary crate
    • programs you can compile to an executable that you can run
    • i.e. command line program or server
    • must have a function "main"
  • library crate
    • don't have a main function
    • don't compile to an executable
    • define functionality intended to be shared with multiple projects
  • crate root
    • source file that the Rust compiler starts from and makes up the root module of your crate
    • src/main.rs
    • src/lib.rs
  • package
    • bundle of one or more crates that provides a set of functionality
    • contains Cargo.toml
      • describes how to build those crates

Defining Modules to Control Scope and Privacy

  • paths
    • allow you to name items
  • use
    • brings a path into scope
  • pub
    • make items public
  • modules
    • organize code within a crate for readability and easy reuse
    • allow control of privacy of items because code within a module is private by default
    • mod
      • how to define a module
      • we can place modules inside modules

Paths for Referring to an item in the Module Tree

  • absolute path
    • full path starting from a crate root; for code from an external crate, the absolute path begins with the crate name, and for code from the current crate, it starts with the literal crate
  • relative path
    • starts from the current module and uses self, super, or an identified in the current module
  • both absolute path and relative pats are followed by one or more identifiers separated by double colons (::)

Exposing Paths with the pub Keyword

  • have to make nested code public as well

Starting Relative Paths with super

  • using "super" allows us to reference an item that we know is in the parent module, which can make rearranging the module tree easier when the module is closely related to the parent

Making Structs and Enums Public

  • if we use "pub" before a struct definition, we make the struct public, but the struct's fields will still be private
  • we can make each field public or not on a case-by-case basis

Bringing Paths into Scope with the use Keyword

  • we can create a shortcut to a path with the use keyword once, and tehn use the shorter name everhwere else in the scope

Creating Idiomatic use Paths

Providing New Names with pub use

Re-exporting Names with pub use

Using External Packages

Using Nested Paths to Clean Up Large use Lists

The glob operator

Separating Modules into Different Files

Summary

8 Common Collections

Storing Lists of Values with Vectors

Stoing UTF-8 Encoded Text with Strings

Storing Keys with Associated Values in Hash Maps