KevsRobots Learning Platform
100% Percent Complete
By Kevin McAleer, 2 Minutes

Cargo is not just a package manager; itβs also a Rust projectβs build system. It manages building your code, downloading the libraries your code depends on, and building those libraries. This bonus lesson will guide you through creating a new Rust project with Cargo, managing dependencies, and building projects.
To start a new Rust project with Cargo, you can use the following command:
cargo new project_name
This command creates a new directory called project_name containing a Cargo.toml file (the manifest file for Rust projects) and a src directory with a main.rs file. The Cargo.toml file will include basic metadata about your project and a list of dependencies.
Add dependencies to your project by listing them under [dependencies] in your Cargo.toml file. For example, to add the serde library:
[dependencies]
serde = "1.0"
When you build your project, Cargo will automatically download and compile your dependencies and all of their dependencies.
To build your project, run the following command in your project directory:
cargo build
This command compiles your project and all of its dependencies. If the build is successful, Cargo places the executable in target/debug/project_name.
To build and run your project in one step, you can use:
cargo run
If your project compiles successfully, Cargo will then run the resulting executable.
In this bonus lesson, you learned how to create a new Rust project using Cargo, manage your projectβs dependencies, and build and run your project. Cargo is a powerful tool that simplifies many aspects of Rust development, making it easier to manage large projects and their dependencies.
You can use the arrows β β on your keyboard to navigate between lessons.
Comments