Cargo is the Rust package manager. Cargo downloads your Rust package's dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io, the Rust community's package registry.
To see if you have cargo installed, use:
Creating a new project
cargo new command is used to create a new project, it creates a directory called
hello_cargo that contains a file called
src folder and a new git repository.
cargo new <project-name>
<project-name> with the name of your project. In this case, we'll use
hello_cargo as the project name.
Inside of the generated directory's
src folder is a file called
main.rs which is the entry point of this Rust program.
Version control system
Rust also takes care of setting up a Version Control System (VCS) for us, the default option is Git. The project's repository folder (
.git) is created in the project's root directory.
To use a different VCS or to disable it entirely, use the
--vcs option with the
cargo new command, the supported version control system's are:
When you use the "none" option, no version control system is initiated and repositories are created. Only the source files (
cargo.toml) files are generated for the project.
Rust expects your source files to live inside of the
src folder. The root folder is for README's, licenses, config files and anything else not related to the source code of your program.
If you started with a project that doesn't use cargo but would like to add cargo to your project, create a new folder called
src and move your project's source code into that folder. Then create a
cargo.toml file and run
cargo build. Make sure your current working directory (CWD) is the root of your project or the folder that contains your
To change the current CWD, use the
cd command followed by the directory path of your project folder's root:
Contents of the
cargo.toml file is a TOML document with 2 headings, the first heading:
[packages] defines a package and specifies program information. 
For example, the
[packages] heading can include information like:
name— The name of the package
version— The version of the package
authors— The authors of the package
rust-version— The minimal supported Rust version
description— A description of the package
documentation— URL of the package documentation
readme— Path to the package's README file
license— The package license
keywords— Keywords for the package
categories— Categories of the package
workspace— Path to the workspace for the package
build— Path to the package build script
default-run— The default binary to run by cargo run
The next heading
[dependencies] states the list of your programs packages, known as crates in rust. Although sometimes you might need multiple types of dependency headings:
[dependencies]— Package library dependencies
[dev-dependencies]— Dependencies for examples, tests, and benchmarks
[build-dependencies]— Dependencies for build scripts
[target]— Platform-specific dependencies
Build, run and check
cargo build command creates an executable from the available source code (if no compiler errors are thrown) but Cargo won't run the program after building it. To build and run a program you'd use the
cargo run command. Both
cargo run and
cargo build will compile the code and create a new executable in the
To compile your project with optimizations for distribution use:
cargo build --release, however, you should keep in mind that
cargo build --release is far slower than
cargo build and should only be used when you actually want to build a release edition of your program.