The Rust programming language (also referred to as rust-lang) is a popular general-purpose programming language, syntactically similar to C++ but more advanced than it.

It offers a lot of features, such as zero-cost abstraction, threads without data races, pattern matching, memory safety, safe memory space allocation, and many more.

Top MNC companies like Figma, Amazon, Discord, Google, and many others have begun using Rust for their products. If you're new to learning a programming language and are torn between learning C++ or Rust, check out this thread.

In this article, I'll guide you on how to install the latest version of Rust on Ubuntu and other Linux distros, and then learn how to create, compile, and run a test program.

How to Install Rust on Ubuntu and Other Linux Distros

1. First, open your terminal and execute the following command to install the curl package required to fetch the Rustup installer script:

  • On Debian and Ubuntu
  • sudo apt install curl
  • On RedHat and Fedora
  • sudo dnf install curl
  • On Arch and Manjaro
  • sudo pacman -S curl

2. Run the following command to install Rust using the Rustup script:

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

During installation, you'll be asked to specify the installation type. For now, I'll select "1", but if you're familiar with the rustup installer and wish to customize, you can choose the "2" option.

rustup installation type

The installation will take some time, allowing you to take a coffee break; thereafter, you'll receive the following output upon completion.

rust installed

3. Once the installation is complete, you will be asked to restart your terminal session or configure the current shell using the following command, so that Cargo's bin directory, $HOME/.cargo/bin, will be added to your PATH environment variable.

  • source "$HOME/.cargo/env"

Output:

configuring the current shell for new changes

4. To verify the Rust installation, you can check its version by running the following command:

  • rustc --version

Output:

rust version

How to Create, Compile, and Run a Test Program in Rust

Once Rust is successfully installed and accessible from the terminal, you can create a test program to verify that it's working properly.

1. First, create a new directory and then move into it.

  • mkdir ~/rustprojects
  • cd ~/rustprojects

Output:

creating project directory for rust

2. Use your preferred text editor to create a file named test.rs, then proceed to copy and paste the following lines of code into the file.

fn main() {
    println!("Welcome to UbuntuShell.com");
}

3. Then run the following command, which will create an executable file named test in the current directory.

  • rustc test.rs

Output:

compiling rust program

4. Finally, execute the test executable file.

  • ./test

Output:

running rust program

Rustup Command-Line Usage

The rustup command offers multiple options to manage the rust. For example, the following command will update Rust to the latest release.

  • rustup update

At any point, if you want to remove Rust, run.

  • rustup self uninstall

During removal, it will ask you to type "Y" to remove all the files associated with Rust and the added Cargo's bin directory, $HOME/.cargo/bin, from your system PATH.

removing rust