Rust (commonly known as Rust-Lang) is a modern, powerful, open-source server-side programming language. It was developed by Mozilla and first launched in 2010. It provides numerous features such as move semantics, zero-cost abstractions, pattern matching, minimal runtime, type inference, threads without data traces, efficient C bindings, and so on. It is employed in data centers by companies like Dropbox, Postmates, Stac, Wantedly, Doctolib, and QIWI, and emphasizes safety, control of memory layout, and concurrency.
Rust is similar to c++ and runs on a number of platforms. Organizations that rely on Rust in their production servers include CoreOS, Dropbox, and Mozilla.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Open Software Installation and Configuration on Linux Systems.
In this context, we shall look into steps to Install Rust language on Linux and write our first program.
1. Install dependencies for Rust
To begin, we need to install the necessary dependencies required for Rust in Linux.
To install the dependencies on Debian-based systems, run the following command:
$ sudo apt-get install build-essential -y
To install the dependencies on Red Hat-based systems, run the following command:
$ sudo dnf install cmake gcc -y
2. Install curl
Now, we will install Rust in Linux using curl, a free command-line utility. If you don’t have curl in your system, run either of the following commands to install.
To install curl on a Ubuntu / Debian-based Linux distributions run:
$ sudo apt install curl -y
To install curl on Red Hat-based Linux distributions such as Rocky Linux, Fedora and CentOS, run:
$ sudo dnf install curl -y
3. Install Rust
With curl installed, the next step will be to install rust. Use the curl command to download and execute the installation script:
$ curl https://sh.rustup.rs -sSf | sh
The above command is distribution-agnostic and will run on any Linux environment.
When prompted, select '1' and hit Enter to proceed with the installation. All the required components for Rust programming language will be downloaded.
Rust will not add the Cargo bin directory (the package management and crate host for rust) to your $PATH during the installation process, therefore you'll have to do it manually. Run the command:
$ source $HOME/.cargo/env
Next, source your user .profile file so as to make sure it uses the modified $PATH. This ensures your shell functions properly within the Rust environment. Execute the command below:
$ source ~/.profile
Finally, let's verify the version of rust installed in our machine with the command:
$ rust --version
You can run your first program in rust. We will run a simple "hello world" script. I will first create a new directory for my rust project:
$ mkdir ~/rustproject
Next, navigate into the new directory:
$ cd rustproject
Let's now create and open a new rust file with the command:
$ sudo nano rusttestfile.rs
You should notice that rust programs are saved with the .rs extension.
Copy the following lines of code that print the statement "This script is running". Save and close the file:
println!(“This rust script is running”);
After saving the above file, create a Rust executable for our rusttestfile.rs file with the command:
$ rustc rusttestfile.rs
Finally, we will run the compiled program with the following command:
You should see "This script is running!" printed in the output upon successful execution of the program.
This article covers the step by step Installation procedure of Rust on Linux. Now you can write rust programs on Linux. In fact, The Rust programming language, more commonly known as rust-lang, is a powerful general-purpose programming language.
Developed by Mozilla and designed by Graydon Hoare, Rust programming language is syntactically similar to C++.
We can verify from the terminal for the version of the Rust programming language installed on our system. To do so, we enter this in our terminal:
$ rustc --version