Day 2: Installing Rust and Setting Up Your Development Environment
Objective
How to Install Rust in Linux?
By the end of this lesson, you will have Rust installed on your machine and a development environment configured to start writing Rust code.
1. Installing Rust
The recommended way to install Rust is through rustup
, which manages Rust versions and associated tools. Here’s how to get started:
Step 1: Install rustup
- Open your terminal (or command prompt) and run the following command to download and install
rustup
:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
- If you're using Windows, you can use PowerShell:
Invoke-WebRequest -Uri https://sh.rustup.rs -UseBasicP -OutFile rustup-init.exe; .\rustup-init.exe
Step 2: Follow the prompts
- The installer will guide you through the installation process. You can generally choose the default options.
Step 3: Update your PATH
- After installation, you need to ensure that the Rust binaries are in your system's PATH. If you installed via
rustup
, this should be done automatically. If not, you might need to add the following line to your shell profile (e.g.,.bashrc
,.zshrc
, or.bash_profile
):export PATH="$HOME/.cargo/bin:$PATH"
Step 4: Verify the installation
- To confirm that Rust is installed correctly, run:
You should see the version of Rust you installed.rustc --version
2. Installing Cargo
Cargo is Rust’s package manager and build system, included with the installation of Rust via rustup
. It helps manage dependencies, compile packages, and more.
- To verify that Cargo is installed, run:
cargo --version
3. Setting Up Your Development Environment
Now that Rust and Cargo are installed, let’s set up a development environment.
Option 1: Use a Text Editor or IDE
You can choose any text editor or Integrated Development Environment (IDE) for Rust development. Here are some popular choices:
- Visual Studio Code (VS Code): A popular, lightweight editor with excellent Rust support via extensions.
- Install the Rust Analyzer extension for enhanced Rust support, including auto-completion, error checking, and more.
Option 2: Install Rust Language Support in Your Editor
-
For VS Code:
- Open VS Code and go to Extensions (Ctrl+Shift+X).
- Search for and install the Rust Analyzer extension.
-
For IntelliJ IDEA:
- Open IntelliJ and navigate to Plugins in the settings.
- Search for Rust and install the plugin.
4. Creating Your First Rust Project with Cargo
With Rust and Cargo installed, let’s create a simple project.
Step 1: Create a new Cargo project
- Run the following command in your terminal:
This command creates a new directory calledcargo new hello_cargo
hello_cargo
with a simple project structure.
Step 2: Navigate to your project directory
- Change to the newly created project directory:
cd hello_cargo
Step 3: Build and run your project
- You can build and run the project using Cargo:
cargo run
You should see the output:
Hello, world!
This message comes from the src/main.rs
file created by Cargo, which contains your main function.
5. Conclusion
Today, you learned how to install Rust and set up your development environment, including installing Cargo and creating your first Rust project. With these tools, you’re now ready to dive deeper into Rust programming.
Next Steps
In the next article, we’ll explore Rust’s basic syntax, including variables, data types, and control flow.