Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Setting Up Rust

1. Introduction

Rust is a systems programming language that focuses on safety, speed, and concurrency. To start programming in Rust, you first need to set it up on your machine. This involves installing the Rust compiler and its package manager, Cargo. This tutorial will guide you through the steps to get Rust up and running on your system.

2. Installing Rust

The recommended way to install Rust is through rustup, a command-line tool for managing Rust versions and associated tools.

2.1. Installation Steps

Follow these steps to install Rust on your system:

Open your terminal and run the following command:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

This command downloads and runs the installation script. You will be prompted to proceed with the installation. Press 1 to proceed with the default installation.

Once the installation is complete, you will see a message similar to:

Rust is installed now. Great!

3. Configuring Your Environment

After installation, you need to configure your shell to use Rust’s tools. This is typically done by adding the Rust binary path to your system's environment variables.

3.1. Adding Rust to your PATH

The installation script will suggest how to add Rust to your PATH. You can follow the instructions provided in the terminal. For example, if you are using Bash, you can add the following line to your .bashrc or .bash_profile file:

export PATH="$HOME/.cargo/bin:$PATH"

After adding this line, run the following command to apply the changes:

source ~/.bashrc

4. Verifying the Installation

To confirm that Rust has been installed correctly, you can check the version of Rust installed on your system by running the following command:

rustc --version

If installed correctly, you should see output similar to:

rustc 1.60.0 (7737e0b5c 2022-04-04)

5. Creating Your First Rust Project

Now that Rust is installed, you can create your first Rust project using Cargo, Rust's package manager and build system.

5.1. Creating a New Project

To create a new Rust project, run the following command:

cargo new hello_rust

This command creates a new directory called hello_rust with a default project structure.

5.2. Navigating to the Project Directory

Change to the project directory:

cd hello_rust

5.3. Building and Running the Project

You can build and run your project using the following command:

cargo run

You should see output that says:

Hello, world!

6. Conclusion

Congratulations! You have successfully set up Rust on your system and created your first Rust project. With Rust's focus on performance and safety, you're now ready to explore further and dive into more advanced features of the language.

For additional resources, you can visit the official Rust website and the Rust Book.