How to install Rust programming language on Ubuntu 20.04 – VITUX

Rust is a modern programming language created and developed at Mozilla Research in 2010 by its namesake author, Brendan Eich. Like most languages, Rust has many built-in primitives that allow you to make your programs more efficient: loops, conditional sentences, string manipulation, etc. However, Rust also enhances these primitives with a modern type of system that guarantees memory security and an innovative design inspired of Haskell’s approach to contemporaneity without sacrificing performance. The result is a fast and secure language that can be used with any other high-level language to build high-performance applications running on any platform without compromising on security or scalability.

Rust is a heavily typed language, which means all your code must be declared before it can be evaluated. This helps guarantee memory security and prevents accidental errors while driving. Rust also uses a garbage collector, which means you do not have to manually free (or delete) memory. Instead, the garbage man automatically takes care of this for you. In addition, Rust has a modern type system that guarantees Memory Safety and provides a set of built-in types that make it possible to create safe, type-safe programs on just one line of code.

Rust has proven to be faster than C ++, but it also uses less memory due to its functional style. This is because Rust does not allocate memory on the heap, but instead uses pointers to store data. In this way, the garbage collector can automatically free up all allocated memory when it is no longer needed. Rust’s type system is designed to work with any other programming language and is used as a secure alternative to C ++.

Updating the system

The first step is to update your system. It is strongly recommended that you always keep your system up to date. This way you will always have the latest security updates installed and your system will be more stable. To do this, open a terminal (Ctrl + Alt + T) and type: sudo apt update && sudo apt upgrade This will update your list of available packages and then install all the updated packages.

sudo apt update -y
sudo apt upgrade -y

After the system is updated, we need to install some important packages that are necessary for Rust. Run the following commands to install the curl, build-essential, gcc, and make package.

sudo apt install curl build-essential gcc make -y

curl is an easy way to download and upload files and web pages. build-essential is a meta-package that contains many development tools needed to compile programs. gcc is the GNU Compiler Collection, which is used to compile Rust code. make is a tool that can be used to create, compile and link programs. The -y setting makes curl download and installs the file automatically. If you omit the -y option, you will be prompted to answer “yes” or “no” each time the curl asks if you want to download and install a new package.

Installation of rust

Now that our system is up to date and has all the necessary packages installed, it’s time to install Rust.

There are several ways to install Rust. You can use the binary, you can use one of the pre-built Debian packages, or you can compile it from the source. We need to install Rust using the finished installation script from the developer.

Using the installation script, we get an already configured system that is ready to go straight out of the box. This will save us a lot of time and effort.

Run the following command to download the Rust installation script.

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

Using curl we can download and install the Rust installation script. The -proto option allows you to specify the protocol that you want the curl to support. https is the protocol used to download the Rust installation script. The –tlsv1_2 setting specifies the digits to use when establishing a connection. This is required so that curl can communicate with HTTPS servers such as rustup.rs.

With the -tlsv1.2 flag, we ensure that the server we connect to uses the latest TLSv1.2 protocol. The -sS setting tells curl that this is a silent download and that it should not produce a status bar or any other output. The final flag -f forces the curl to continue the transfer, even if it detects an unknown error.

The installation script is downloaded from https://sh.rustup.rs and executed as a shell script. the sh argument tells the shell that it should be executed as a shell script.Advertising

You will be prompted to select the installation options. It is recommended to select the default settings unless you have a good reason not to. Type 1 and press Enter.

Install Ruts using Rustup

The installation process may take some time (approximately 10-15 minutes) depending on the speed of your internet connection and your server loader. When the installation is complete, you will be presented with an overview of the installation process.

Rust has been installed successfully

Finally, run the rustc -V command to confirm that Rust has been installed correctly. If you see an output similar to the one below, everything works fine.

Rustc is the Rust compiler and is used to compile code written in the Rust programming language. -V flag tells rustc to print some basic information about itself.

The version number displayed in the output is the current stable Rust release. The unstable release of Rust also has a version number. However, it is prefixed with a large U.

However, we are not done yet. You must run two more commands before you can start using Rust.

Run the source ~ / .profile command to update the system environment variables. We use the source command to execute a shell script that modifies the system environment variables. We do this to keep the PATH system variable up to date, which is necessary for the Rust compiler to work properly.

source ~/.profile

System environment variables are used to store values ​​used by the system. Some common environmental variables are:

  • HOME: This variable contains your home directory.
  • PATH: This variable contains a list of folders that are searched for when you try to run an application.
  • TERM: This variable contains information about the terminal type.

Then run the source command ~ / .cargo / env to update the load environment variables. Rust uses Cargo to manage packages. The load variables are used by the Rust compiler to locate where the package’s binaries are located.

source ~/.cargo/env

Finally, run the rustc -V command to confirm that Rust has been installed correctly. If you see an output similar to the one below, everything works fine.

Rustc is the Rust compiler and is used to compile code written in the Rust programming language. -V flag tells rustc to print some basic information about itself.

rustc -V

The version number displayed in the output is the current stable Rust release. The unstable release of Rust also has a version number. However, it is prefixed with a capital U.

Test your rust installation

Now that you’ve installed Rust, it’s time to test your installation. We need to test your installation by running a simple program called “hello world”. This program will print a message saying “hello world” to the screen.

The Hello world program is a computer program that broadcasts or displays the words “hello world” to the user. This statement is used as output to test a new compiler, programming language, operating system, or piece of software. It is a simple program used to ensure that everything works properly on a new system.

The goal of this demo is to first introduce you to simple concepts, and then you can gradually move into more advanced topics.

First, we need to create a new folder to store the source code for the Hello World program. Run the mkdir hello command to create a new directory called hello.

mkdir hello

Then use the CD to enter the newly created folder.

cd hello

Then run the sudo nano hello-rust.rs command to open the nano-text editor and create a new file called hello-rust.rs.

sudo nano hello-rust.rs

The Hello-rust.rs file is the source code of the “hello world” program. Copy and paste the following code into this file.

fn main() {
  println!("Hello, This is a text provided by Vitux");
}

fn main is the main feature of the “hello world” program. fn main corresponds to the main function in C and other programming languages. But unlike other languages, the function name is always in lower case.

The printout! macro prints text to the screen. The string between the two percent characters (Hi, this is a text provided by Vitux) will be printed on the screen.

After entering the code, press CTRL + X to exit the nano-editor. Press Y for yes, then press ENTER to save the file.

Now that we have the program “hello world” in our hello-rust.rs file, it is time to compile this program into a binary. Run the rustc hello-rust.rs command to compile the source code of a binary.

rustc hello-rust.rs

The binary is a program that the computer understands and can run. It contains all the code required to run the program. Your source code needs to be converted to a more primitive language before it can be read by your computer. This conversion is called compilation.

Once rustc hello-rust.rs has been compiled, run the ls command to display all the files in the folder. You should now see a new file called hello-rust inside your hello-library.

ls

Rust test script

Finally, run the command./hello-rust to execute the program.

./hello-rust

This command will run the “hello world” program and it should print a string on the screen. Congratulations! You have successfully installed and run your first Rust application on Debian 11.

Hello world in Rust

Conclusion

We hope this guide helps you install Rust on your Debian 11 machine as well as helps you gain a good understanding of how the Rust compiler works. For more information on Rust, we recommend that you visit http://www.rust-lang.org/

Leave a Comment