105800 Views
82496 Views
47243 Views
47066 Views
45458 Views
38527 Views
Pi-Apps
Intermediate MicroPython
Arduino Alvik
Raspberry Pi Time machine
Now Ad-Free
Guiding Light
Intermediate level MicroPython
Introduction to FreeCAD for Beginners
Building a Robot Arm with Raspberry Pi and PCA9685
Building User Authentication for Static Sites with FastAPI
Mastering Pydantic for Robust Data Validation
Mastering Markdown for Documentation with Jekyll
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.
project_name
Cargo.toml
src
main.rs
Add dependencies to your project by listing them under [dependencies] in your Cargo.toml file. For example, to add the serde library:
[dependencies]
serde
[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.
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.
< Previous