If Rust is installed on your system, then
cargo by default is installed.
cargo is Rust's package
management system and can be called from the command line.
Creating a new project with Cargo
Create a new project using
cargo using the
cargo new command. You will need to have your command
line/terminal open. For the sake of this course, let's call the project
first-cargo, which will
create a new directory with your Rust project within:
cargo new first-cargo
ls -R, the file structure is revealed to be the following:
2 directories, 2 files
src/is where your project lives and contains the source of your Rust project.
main.rsis where you can and will write executable code.
Cargo.tomlis how your Rust project manages project metadata, dependencies, and build options.
Cargo.toml is written in the TOML format, and specifies metadata for your Rust project. Upon
inspecting our project,
Cargo.toml it should look relatively barebones:
name = "first-cargo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Each section is defined by square brackets (
) followed by the name within (i.e.,
Keys are determined by the name, followed by an equals sign (
=) and the value as text in double
name = "first-cargo").
[package]- Specifies the name of the package, version of the binary, and the Rust edition used.
[dependencies]- Specifies a list of local or remote external dependencies.
Building & Running with Cargo
cargo build within any directory with
Cargo.toml will result in an attempt to build the
project. This merely builds the project and generates a
target/ folder with the compiled binary.
Compiling first-cargo v0.1.0 (/rust-course/first-cargo)
Finished dev [unoptimized + debuginfo] target(s) in 1.09s
cargo run will both build the project, as well as run the binary.
Finished dev [unoptimized + debuginfo] target(s) in 0.03s