Skip to content

Commit

Permalink
updates to ch. 7
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhoffa committed Nov 25, 2024
1 parent c49e3df commit a32bc66
Showing 1 changed file with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ title: "7. Managing Growing Projects with Packages, Crates, and Modules"

# Learning objectives

By the end of this session, you should be able to:

::: nonincremental
- Understand and use Rust's module system (`mod`, `pub`, `use`).
- Differentiate between packages, crates, and modules.
- Understand and use Rust's module system (`mod`, `pub`, `use`).
- Implement Cargo workspaces for multi-crate projects.
- Apply best practices for managing and organizing Rust projects.
:::
Expand All @@ -22,10 +20,10 @@ Encourage questions about any points that feel unclear.

## Key Concepts

- **Package**: A bundle of one or more crates, defined by a `Cargo.toml` file.
- **Crate**: A compilation unit in Rust (binary or library).
- **Package**: A Cargo feature that lets you build, test and share crates. Defined by a `Cargo.toml` file.
- **Crate**: A tree of modules that produces a library or executable. Rust's compilation unit.
- **Module**: A way to organize code and control scope and visibility.
- **Workspace**: A collection of interrelated packages managed together.
- **Paths**: A way of naming an item, such as a struct, function, or module.

# Creating and Using Packages

Expand All @@ -39,19 +37,13 @@ $ cd my_project
- Creates a new package with a `Cargo.toml` and a default binary crate.
- Binary crate root: `src/main.rs`.
- To create a library crate, add a `src/lib.rs`.

## Example `Cargo.toml`
``` toml
[package]
name = "my_project"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.5"
```
- An executable crate must have a `main` function as an entry point.
- A library crate can be used by other crates, and does not need a `main` entry point.

# Working with Modules
## Backyard example
- Check out the backyard example!

## Accessing Module Items
``` rust
crate::front_of_house::hosting::add_to_waitlist();
Expand All @@ -65,23 +57,36 @@ front_of_house::hosting::add_to_waitlist();

## Controlling Visibility
``` rust
pub mod front_of_house {
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}

pub fn eat_at_restaurant() {
// Absolute path
crate::front_of_house::hosting::add_to_waitlist();

// Relative path
front_of_house::hosting::add_to_waitlist();
}
```
- By default, items are private
- Use `pub` to make modules, functions, and fields accessible

## No private code in R
- In R, everything is public
- Even un-exported functions can be accessed with `:::`.
- In Rust, private items are not accessible outside their module.

# Using `use` for Convenience
## Simplify Paths
``` rust
use crate::front_of_house::hosting;

hosting::add_to_waitlist();
```
- Use use to shorten paths.
- Use `use` to shorten paths.
- Works within the scope it's defined.

## Re-exporting with `pub use`
Expand Down

0 comments on commit a32bc66

Please sign in to comment.