Package Management with Cargo

Hands-on Rust — by Herbert Wolverson (20 / 120)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Finding Common Mistakes with Clippy | TOC | Wrap-Up 👉

Cargo can install dependencies for you. There’s an ever-growing number of “crates” (Rust’s name for packages) available for free on the crates.io system.[17] Cargo makes it easy to install and use these crates, as well as to publish your own.

You can search available crates by typing cargo search [search term] or visiting the crates website. For example, searching for bracket-terminal produces the following:

​=> ​cargo search bracket-terminal​
​<= bracket-terminal = "0.7.0" # ASCII/Codepage 437 terminal emulator with a
​ game loop. Defaults to OpenGL, also support
​ Amethyst,…

Searching also searches crates’ descriptions. For example, if you find yourself needing a “slot map” — a few crates appear to offer one:

​=> ​cargo search slotmap​
​<= slotmap = "0.4.0" # Slotmap data structure
​ beach_map = "0.1.2" # Implementation of a slotmap

Once you’ve found a crate you wish to use, add it to Cargo.toml. In the [dependencies] section, add a line for the dependency and version:

​ ​[dependencies]​
​ bracket-lib = ​"0.8.0"​

Version numbers use semantic versioning, just like your project’s version number.[18]

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.