[Setup] Rust Development Environment
This page walks you through setting up a Rust development environment. Rust can be easily installed using the official tool rustup.
Installation (rustup)
rustup is the official Rust installer and version management tool. Install it with the following command.
For macOS / Linux
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the on-screen instructions to complete the installation. When done, either restart your terminal or run the following to apply the PATH settings.
source $HOME/.cargo/env
For Windows
Download and run the installer from rustup.rs. Visual Studio C++ build tools are required, so follow the instructions to install them.
Verifying the Installation
rustc --version cargo --version
If both versions are displayed, the installation is complete.
Creating a Project with Cargo
Cargo is Rust's official build tool and package manager. All project creation, building, running, and dependency management is handled through Cargo.
1. Create a project
cargo new hello cd hello
The following files are auto-generated.
| File | Description |
|---|---|
| src/main.rs | The main source file. |
| Cargo.toml | The file that describes the project configuration and dependencies. |
2. Review the source code
Open the auto-generated src/main.rs and you will see the following code.
fn main() {
println!("Hello, world!");
}
3. Build and run
cargo run
If Hello, world! is displayed, you're all set. cargo run compiles and runs in one step.
Compiling Directly with rustc
You can also compile directly using the rustc command without Cargo.
Create hello.rs.
fn main() {
println!("Hello, World!");
}
rustc hello.rs ./hello
This is convenient for quickly checking simple programs, but use Cargo when you need external libraries.
Commonly Used Cargo Commands
| Command | Description |
|---|---|
| cargo new project-name | Creates a new project. |
| cargo run | Builds and runs. |
| cargo build | Builds only (does not run). |
| cargo build --release | Generates an optimized executable. |
| cargo test | Runs tests. |
| cargo fmt | Formats the code. |
| cargo clippy | Detects code issues (linter). |
If the Command Is Not Found
If your terminal displays rustc: command not found or cargo: command not found, the PATH may not be configured correctly. Follow the steps below to check and fix the issue.
1. Find the command location
Check where the command is located.
which rustc which cargo
If not found, check the default rustup installation location.
ls ~/.cargo/bin/rustc ls ~/.cargo/bin/cargo
2. Check which shell you are using
echo $SHELL
If /bin/zsh is shown, edit ~/.zshrc; if /bin/bash is shown, edit ~/.bashrc.
3. Add to PATH
When installed via rustup, Rust commands are placed in ~/.cargo/bin/. Add the following to your shell configuration file.
For macOS (zsh):
echo 'source $HOME/.cargo/env' >> ~/.zshrc source ~/.zshrc
For Linux (bash):
echo 'source $HOME/.cargo/env' >> ~/.bashrc source ~/.bashrc
$HOME/.cargo/env is a script auto-generated by rustup that configures the PATH. Loading it with source applies the changes to your current shell immediately.
For Windows, the rustup installer sets the PATH automatically. If it is not working, try restarting Command Prompt.
If you find any errors or copyright issues, please contact us.