Rust Learning Series
Rust Installation
Rust Language
Installation on Unix-like OS
For installing on macOS, Linux, or another Unix-like OS:
curl https://sh.rustup.rs -sSf | sh
The above command downloads a script and starts the installation of the rustup
tool which installs the latest stable version of Rust.
Rust, by default, installed in the following location:
$HOME/.cargo
Following are the directories inside $HOME/.cargo
.
$HOME/.cargo
|- bin/
|- env/
|- registry/
rustup
Rust and its associated tools are installed and managed by a command line tool called rustup
.
Find rustup Version
rustup --version
Update rustup
rustup update
Uninstall Rust and rustup
rustup self uninstall
Troubleshooting
To check whether we have Rust installed correctly, run the below rustup command in the terminal:
rustup --version
Toolchain
In the Rust development environment, all tools are installed to the $HOME/.cargo/bin
directory and this is where you will find the Rust toolchain, including rustc
, cargo
, and rustup
.
Keeping Rust up to date
The Rust release process adopts release train model in which there are three different release channels: nightly, beta, and stable. Nightly gets promoted to beta and beta gets promoted to stable: nightly → beta → stable.
rustup
is configured to use the stable channel by default and is released every six weeks. When a new version of Rust is released, we can type rustup update
to update to it:
$ rustup update
info: syncing channel updates for 'stable'
info: downloading component 'rustc'
info: downloading component 'rust-std'
info: downloading component 'rust-docs'
info: downloading component 'cargo'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'rust-docs'
info: installing component 'cargo'
info: checking for self-updates
info: downloading self-updates stable updated: rustc 1.7.0 (a5d1e7a59 2016-02-29)
Updating rustup alone (Self update)
Running rustup update
also checks for updates to rustup
and automatically installs the latest version. To manually check for updates and install the latest version of rustup
without updating installed toolchains, type rustup self update
.
$ rustup self update
info: checking for self-updates
info: downloading self-updates
Updating Rust Toolchain alone (without updating rustup)
Note that rustup
will automatically update itself at the end of any toolchain installation as well. You can prevent this automatic behaviour by passing the --no-self-update
argument when running rustup update
or rustup toolchain install
.
Environment variables
RUSTUP_HOME
(default: ~/.rustup
or %USERPROFILE%/.rustup
)
RUSTUP_TOOLCHAIN
(default: none)
RUSTUP_DIST_SERVER
(default: https://static.rust-lang.org
)
RUSTUP_UPDATE_ROOT
(default https://static.rust-lang.org/rustup
)
RUSTUP_IO_THREADS
unstable (defaults to reported CPU count)
RUSTUP_TRACE_DIR
unstable (default: no tracing)
RUSTUP_UNPACK_RAM
unstable (default 400M, min 100M)
RUSTUP_NO_BACKTRACE
RUST_BACKTRACE
Cargo
Cargo is the Rust build tool and package manager. It comes installed with Rust if we use the official installation steps.
Cargo does lots of things:
- Build our project with
cargo build
— takes care of downloading the libraries our code depends on. - Run our project with
cargo run
- Test our project with
cargo test
- Build documentation of our project with
cargo doc
- Publish a library to crates.io with
cargo publish
Other Tools
Rustfmt
It’s a formatting tool and can be installed via rustup as follows rustup component add rustfmt
Clippy
It’s a lint tool which can be installed via rustup as follows rustup component add clippy