The Rust Programming Language: A Comprehensive Guide
Introduction
The Rust programming language, also known as rust-lang, is a powerful general-purpose programming language. Rust is syntactically similar to C++ and is used for a wide range of software development projects, including browser components, game engines, and operating systems.
Overview of This Tutorial
In this tutorial, you’ll install the latest version of Rust on Ubuntu 20.04, and then create, compile, and run a test program. The examples in this tutorial show the installation of Rust version 1.66.
Note: This tutorial also works for Ubuntu 22.04, however, you might be presented with interactive dialogs for various questions when you run apt upgrade. For example, you might be asked if you want to automatically restart services when required or if you want to replace a configuration file that you’ve modified. The answers to these questions depend on your software and preferences and are outside the scope of this tutorial.
Prerequisites
To complete this tutorial, you’ll need an Ubuntu 20.04 server with a sudo-enabled non-root user and a firewall.
Step 1 — Installing Rust on Ubuntu Using the rustup Tool
This tutorial uses the default option 1. However, if you’re familiar with the rustup installer and want to customize your installation, you can choose option 2. Type your selection and press Enter.
The output for option 1 is:
info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2023-01-10, rust version 1.66.1 (90743e729 2023-01-10)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
67.4 MiB / 67.4 MiB (100 %) 40.9 MiB/s in 1s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
6.6 MiB / 6.6 MiB (100 %) 5.5 MiB/s in 1s ETA: 0s
info: installing component 'clippy'
info: installing component 'rust-docs'
19.1 MiB / 19.1 MiB (100 %) 2.4 MiB/s in 7s ETA: 0s
info: installing component 'rust-std'
30.0 MiB / 30.0 MiB (100 %) 5.6 MiB/s in 5s ETA: 0s
info: installing component 'rustc'
67.4 MiB / 67.4 MiB (100 %) 5.9 MiB/s in 11s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
stable-x86_64-unknown-linux-gnu installed - rustc 1.66.1 (90743e729 2023-01-10)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include Cargo’s bin directory ($HOME/.cargo/bin
).
To configure your current shell, run:
source "$HOME/.cargo/env"
Step 2 — Verifying the Installation
Next, run the following command to add the Rust toolchain directory to the PATH environment variable:
source $HOME/.cargo/env
Verify the Rust installation by requesting the version:
rustc --version
The rustc --version
command returns the version of the Rust programming language installed on your system. For example:
Output
sammy@ubuntu:~$ rustc --version
rustc 1.66.1 (90743e729 2023-01-10)
Step 3 — Installing a Compiler
Rust requires a linker program to join compiled outputs into one file. The GNU Compiler Collection (gcc) in the build-essential package includes a linker. If you don’t install gcc, then you might get the following error when you try to compile:
error: linker `cc` not found
|
= note: No such file or directory (os error 2)
error: aborting due to previous error
You’ll use apt
to install the build-essential
package.
First, update the Apt package index:
sudo apt update
Enter your password to continue if prompted. The apt update
command outputs a list of packages that can be upgraded. For example:
Output
sammy@ubuntu:~$ sudo apt update
[sudo] password for sammy:
Hit:1 http://mirrors.digitalocean.com/ubuntu focal InRelease
Get:2 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease [114 kB]
...
Fetched 11.2 MB in 5s (2131 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
103 packages can be upgraded. Run 'apt list --upgradable' to see them.
Next, upgrade any out-of-date packages:
sudo apt upgrade
When the upgrades are complete, install the build-essential package:
sudo apt install build-essential
Step 4 — Creating, Compiling, and Running a Test Program
In this step, you’ll create a test program to try out Rust and verify that it’s working properly.
Start by creating some directories to store the test script:
mkdir ~/rustprojects
cd ~/rustprojects
mkdir testdir
cd testdir
Use nano
or your favorite text editor to create a file in testdir
to store your Rust code:
nano test.rs
Copy the following code into test.rs
and save the file:
fn main() {
println!(\"Congratulations! Your Rust program works.\");
}
Compile the code using the rustc
command:
rustc test.rs
Run the resulting executable:
./test
The program prints to the terminal:
Congratulations! Your Rust program works.
Other Commonly-Used Rust Commands
It’s a good idea to update your installation of Rust on Ubuntu regularly.
Enter the following command to update Rust:
rustup update
You can also remove Rust from your system, along with its associated repositories.
Enter the following command to uninstall Rust:
rustup self uninstall
You’re prompted to enter Y to continue the uninstall process:
Output
sammy@ubuntu:~/rustprojects/testdir$ rustup self uninstall
Thanks for hacking in Rust!
This will uninstall all Rust toolchains and data, and remove
$HOME/.cargo/bin from your PATH environment variable.
Continue? (y/N)
Enter Y to continue:
Continue? (y/N) y
info: removing rustup home
info: removing cargo home
info: removing rustup binaries
info: rustup is uninstalled
Rust is now removed from your system.
Conclusion
Now that you’ve installed and tested out Rust on Ubuntu, continue your learning with more Ubuntu tutorials.