EN | DE

Stage 2.5: Managing Dependencies With Cargo

At first, you have to create a workspace (project) with either cargo new my-project or cargo init.
This will create a Cargo.toml file in the root folder. This is your centralized configuration file for your Rust program. Within that file you can pass meta data like author or a version and it's also used to manage the dependencies for your program.

Unlike other package manager (like npm for example) there is no CLI command to handle dependencies. In Rust you have to add it to Cargo.toml and the compiler will take care of them.

[dependencies]
git2 = "0.13.18"
webbrowser = "0.5.5"
zip = "0.5"
        

This will add the 3 dependencies git2, webbrowser and zip to your project. The next time you compile your program, those dependencies will get downloaded and compiled.

To search for packages, or crates how Rust calls it, you can:

  • run cargo search crate-name
  • open the Rust community’s crate registry: https://crates.io
  • open the unofficial crates.io alternative which offers categories for crates: https://lib.rs

To update your dependencies, you simply run: cargo update

In the next lesson you will learn how to add cargo subcommands (sometimes called Rust binarier).

Rust mascot Ferris