πŸ“¦
World 7 Β· Keeping Things Tidy

Packages & Crates

Imagine a desk drawer crammed with everything you own. πŸ˜… If it’s all in one big pile, it’s nearly impossible to find the one thing you need. But if you sort things into labeled boxes, and line those boxes up on a shelf, suddenly everything is easy to find.

Rust likes things tidy too. As your programs grow bigger, Rust gives you neat ways to organize your code so it never turns into a giant messy pile. Let’s meet the boxes and the shelves of Rust. πŸ“¦

A crate is a labeled box

A crate is one bundle of Rust code. It’s like a single labeled box that holds related things together.

Think of it like this… A crate is a labeled box. Everything inside belongs together β€” one box holds your photos, another holds your notes. Rust code lives in crates the same way. 🧰

There are two kinds of crates, and they have different jobs:

  • πŸƒ A binary crate is one you can run. It has a special starting spot called main. When you press Run, the computer looks for main and begins there.
  • πŸ“š A library crate doesn’t run by itself. Instead, it holds helpful code that other crates can borrow and use, like a shelf of tools everyone shares.
Ferris says: If a crate has a main function, it can run. If it doesn't, it's a library waiting to share its code with anyone who needs it. πŸ¦€

A package is a shelf of boxes

A package is bigger than a crate. A package is one or more crates kept together, plus a special little list called Cargo.toml.

New word Cargo.toml is a tiny file that describes your package β€” its name, its version, and what other crates it needs. Think of it as the label on the front of your shelf. 🏷️

So the picture looks like this:

  • The shelf is your package.
  • The boxes on the shelf are your crates.
  • The label on the shelf is Cargo.toml.
The Big Idea A crate is one bundle of Rust code (a labeled box). A package is one or more crates plus a Cargo.toml (a shelf with a label). Binary crates run; library crates share.
Try this! Look around you. Can you spot something that works like a crate (a box that holds things) and something that works like a package (a shelf that holds boxes)? Tidy code works exactly the same way. ✨

Quick quiz

Which kind of crate can you run because it has a main?

Yes! A binary crate has main, so the computer knows where to start running. πŸŽ‰

You learned… A crate is a labeled box of code, a package is a shelf of crates with a Cargo.toml label, binary crates run and library crates share. Next up: splitting a crate into rooms with Modules! πŸšͺ