chapter two

2 Project management with Cargo

 

This chapter covers

  • Introduction to Cargo
  • Managing Rust projects with Cargo
  • Publishing Rust applications and libraries
  • Following the Rust community’s best practices for managing and publishing projects

Rust’s package management tool is called Cargo, and it’s the interface to Rust’s compiler rustc, the crates.io registry, and many other Rust tools (which are covered in more detail in _ 3_). Strictly speaking, it’s possible to use Rust and rustc without using Cargo, but it’s not something I recommend.

When working with Rust, you’ll likely spend a lot of time using Cargo and tools that work with Cargo. It’s important to familiarize yourself with its use and best practices. In 3, I’ll provide recommendations and details on how to increase the usefulness of Cargo with community crates.

2.1 Cargo tour

To demonstrate Cargo’s features, let’s walk through a tour of Cargo and its typical usage. I implore you to follow along (ideally by running the commands as demonstrated below). In doing so, you may discover new features even if you’re already familiar with Cargo and its usage.

2.1.1 Basic usage

To start, run cargo help to list available commands:

2.1.2 Creating a new application or library

2.1.3 Building, running, and testing

2.1.4 Switching between toolchains

2.2 Dependency management

2.2.1 Handling the Cargo.lock file

2.3 Feature flags

2.4 Patching dependencies

2.4.1 Indirect dependencies

2.4.2 Best practices

2.5 Publishing crates

2.5.1 CI/CD integration

2.5.2 Best practices

2.6 Linking to C libraries