Here we want to give a brief overview of what Cargo is and how it fits into the wider ecosystem.
Installation
Please follow the instructions on https://rustup.rs/.
Run the following in your terminal, then follow the onscreen instructions.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
This will give you the
- Cargo build tool (cargo)
- the Rust compiler (rustc)
- rustup, a command line utility that you can use to install/switch toolchains, setup cross compilation, etc.
Troubleshooting
To check whether you have Rust installed correctly, open a shell and enter this line:
$ rustc --version
You should see the version number, commit hash, and commit date for the latest stable version that has been released, in the following format:
rustc x.y.z (abcabcabc yyyy-mm-dd)
(base) [yanboyang713@archlinux ~]$ cargo --version
cargo 1.71.0 (cfd3bbd8f 2023-06-08)
If you see this information, you have installed Rust successfully! If you don’t see this information, check that Rust is in your %PATH% system variable as follows. In Linux and macOS, use:
$ echo $PATH
If that’s all correct and Rust still isn’t working, there are a number of places you can get help. Find out how to get in touch with other Rustaceans (a silly nickname we call ourselves) on the community page.
The Rust Ecosystem
The Rust ecosystem consists of a number of tools, of which the main ones are:
- rustc: the Rust compiler which turns .rs files into binaries and other intermediate formats.
- cargo: the Rust dependency manager and build tool. Cargo knows how to download dependencies, usually hosted on https://crates.io, and it will pass them to rustc when building your project. Cargo also comes with a built-in test runner which is used to execute unit tests.
- rustup: the Rust toolchain installer and updater. This tool is used to install and update rustc and cargo when new versions of Rust is released. When a new version of Rust is released, simply type rustup update to update. In addition, rustup can also download documentation for the standard library. You can have multiple versions of Rust installed at once and rustup will let you switch between them as needed.
Notes:
- Rust has a rapid release schedule with a new release coming out every six weeks. New releases maintain backwards compatibility with old releases — plus they enable new functionality.
- There are three release channels: “stable”, “beta”, and “nightly”.
- New features are being tested on “nightly”, “beta” is what becomes “stable” every six weeks.
- Dependencies can also be resolved from alternative registries, git, folders, and more.
- Rust also has editions: the current edition is Rust 2021. Previous editions were Rust 2015 and Rust 2018.
- The editions are allowed to make backwards incompatible changes to the language.
- To prevent breaking code, editions are opt-in: you select the edition for your crate via the Cargo.toml file.
- To avoid splitting the ecosystem, Rust compilers can mix code written for different editions.
- Mention that it is quite rare to ever use the compiler directly not through cargo (most users never do).
- It might be worth alluding that Cargo itself is an extremely powerful and comprehensive tool. It is capable of many advanced features including but not limited to:
- Project/package structure
- workspaces
- Dev Dependencies and Runtime Dependency management/caching
- build scripting
- global installation
- It is also extensible with sub command plugins as well (such as cargo clippy).
Subcommand
Read more from the official Cargo Book.