Installing Rust and Cargo
In this lesson, you will learn how to install Rust on your system and understand the role of Cargo, Rust’s official build and package management tool.
A proper Rust installation is essential because all Rust development depends on the Rust compiler and Cargo working together.
What Is Cargo?
Cargo is Rust’s official build system and package manager. It comes bundled with Rust and is installed automatically.
Cargo helps you:
- Create new Rust projects
- Build and run programs
- Manage dependencies (external libraries)
- Run tests and benchmarks
Almost all Rust projects use Cargo.
Installing Rust on Windows
On Windows, Rust is installed using the official Rust installer.
Follow these steps:
- Visit the official Rust website: https://www.rust-lang.org
- Click the Install button
- Download and run the installer
- Follow the on-screen instructions
The installer automatically sets up Rust, Cargo, and the required system paths.
Installing Rust on macOS
On macOS, Rust is installed using a command-line installer.
Open the Terminal and run the following command:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the prompts shown in the terminal. Once completed, Rust and Cargo will be installed.
Installing Rust on Linux
Rust installation on Linux is similar to macOS.
Run the following command in the terminal:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
This command installs rustup, the official Rust version manager.
Verifying the Installation
After installation, you should verify that Rust and Cargo are working correctly.
Run the following commands:
rustc --version
cargo --version
If Rust is installed correctly, you will see version numbers printed in the terminal.
What Is rustup?
rustup is the Rust toolchain installer and version manager.
It allows you to:
- Update Rust to the latest version
- Switch between Rust versions
- Install additional components like documentation
Most developers manage Rust entirely through rustup.
Updating Rust
Rust releases updates frequently. You can update Rust with a single command:
rustup update
This ensures you always have the latest features and security improvements.
Installing Rust Documentation
Rust provides excellent offline documentation. You can install it using:
rustup component add rust-docs
Once installed, you can open the documentation locally in your browser.
Creating Your First Cargo Project
Cargo makes it easy to create new Rust projects.
Run the following command:
cargo new hello_rust
cd hello_rust
This creates a new Rust project with a standard folder structure.
Project Structure Explained
A newly created Cargo project contains:
Cargo.toml– Project configuration and dependenciessrc/main.rs– Main source file
Cargo handles compilation, dependency resolution, and execution automatically.
Running the Project
To build and run the project, use:
cargo run
Cargo compiles the code and runs the program in one step.
📝 Practice Exercises
Exercise 1
Install Rust on your system and verify the installation.
Exercise 2
Create a new Cargo project named my_rust_app.
Exercise 3
Run the default Rust program using Cargo.
✅ Practice Answers
Answer 1
Use rustc --version and cargo --version to verify installation.
Answer 2
cargo new my_rust_app
Answer 3
cd my_rust_app
cargo run
What’s Next?
Now that Rust and Cargo are installed and working, you are ready to learn how Cargo projects are structured in detail.
In the next lesson, you will explore Cargo project files and understand how Rust projects are organized.