Rust develpment enviroment
## 1. Install Rust via rustup
🦀
The best way to install Rust is by using rustup
, the official tool that manages your Rust versions and associated tools.
On macOS, Linux, or other Unix-like systems:
Open your terminal and run the following command. It will download a script and start the installation.
Bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the on-screen instructions. It will typically ask you to choose an installation option. The default (1
) is perfectly fine.
On Windows:
- Go to the official
rustup
site: https://rustup.rs - Download and run
rustup-init.exe
. - Important Prerequisite:
rustup
on Windows depends on the Microsoft C++ build tools. The installer will check if you have them. If not, you’ll need to install the “C++ build tools” from the Visual Studio Installer. The Rust installer provides helpful prompts to guide you through this.
After installation on any OS, close and reopen your terminal and verify that the compiler (rustc
) and package manager (cargo
) are installed:
Bash
rustc --version
cargo --version
You should see the version numbers for both, confirming the installation was successful.
## 2. Configure Your Code Editor (VS Code)
While you can write Rust in any text editor, using a code editor with IDE support will make your life much easier. Visual Studio Code is the most popular choice.
- Install VS Code: If you don’t already have it, download it from https://code.visualstudio.com/.
- Install the
rust-analyzer
Extension: This is the official Language Server Protocol (LSP) for Rust. It provides all the modern features you need.- Open VS Code.
- Go to the Extensions view (
Ctrl+Shift+X
). - Search for
rust-analyzer
. - Click “Install”.
That’s it! rust-analyzer
will automatically start working whenever you open a Rust project (.rs
files).
## 3. Create and Run a “Hello, World!” Project
Let’s make sure everything works together by creating and running a simple program using Cargo, Rust’s package manager and build tool.
- Create a new project: Open your terminal and run:Bash
cargo new hello_world
This command creates a new directory namedhello_world
with a basic project structure, including asrc/main.rs
file and aCargo.toml
configuration file. - Navigate into the project directory:Bash
cd hello_world
- Run the project:Bash
cargo run
Cargo will compile yourmain.rs
file and then run the resulting executable. You should see the following output in your terminal:Hello, world!
You now have a fully functional Rust development environment! 🚀
## Your Core Development Tools
Here’s a quick summary of the tools you just installed:
rustup
: The installer and updater. To update your Rust installation in the future, just runrustup update
.rustc
: The actual Rust compiler. You’ll rarely use this directly.cargo
: The heart of your workflow. It’s the project manager and build tool. You’ll use it to:cargo new
: Create new projects.cargo run
: Compile and run your project.cargo build
: Compile your project without running it.cargo check
: Quickly check your code for errors without compiling. This is much faster thancargo build
.cargo test
: Run your project’s tests.cargo add <crate_name>
: Add third-party libraries (called “crates”) to your project.
rust-analyzer
: The “brains” in your editor that provides autocompletion, go-to-definition, inline errors, and other IDE features.