Hello world in Rust on Linux

The Rust programming language first appeared in 2015 and has rapidly risen in popularity over the years. As of late 2022, it has become officially supported in the development of the Linux kernel, alongside long time staples C and assembly. It is a general purpose language that can be suitable for many types of projects, with advantages like performance and memory safety.

In this tutorial, we will cover the step by step instructions to install Rust on a Linux system. After it is installed, we will code a simple Hello World program in Rust, compile it, and run it. Are you ready to get started programming with Rust?

In this tutorial you will learn:

  • How to install Rust on all Linux distributions
  • How to install a linker
  • How to write a Hello World program in Rust
  • How to compile and execute a Rust program
Hello world in Rust on Linux
Hello world in Rust on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Rust
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Installing Rust in Linux




The Rust developers have released a script which will automatically install Rust on any Linux distribution. This streamlines the installation process by doing all of the legwork for us.

  1. Open a command line terminal and execute the following command to download the setup script with the curl command:
    $ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
    
  2. The installation script will present you with some options. Choose 1 and press Enter:
    Proceed with the Rust installation in Linux
    Proceed with the Rust installation in Linux
  3. The installer will download a few components and proceed with the installation. Everything should conclude within a few minutes or less:
    The installer will confirm that the Rust installation is now complete
    The installer will confirm that the Rust installation is now complete
  4. To make your current session usable with the Rust installation, make sure that you Cargo’s bin directory in your PATH environment variable:


    $ source "$HOME/.cargo/env"
    

Creating a Hello World script in Rust

After the installation is complete, we are ready to start programming with Rust. As is the starting point with any new language you are about to learn, we will create a simple Hello World script. This will help us verify that the installation was successfully configured, and give you a basic idea of how the Rust language works.

  1. First, it is recommended to create a new directory to house your Rust project:
    $ mkdir hello-world
    $ cd hello-world
    
  2. Next, create a new file named main.rs which contains the following Hello World code:
    fn main() {
        println!("Hello, world!");
    }
    

    Save the file and exit when you are done making the changes.

  3. Then, we will use the rustc command to compile the Rust script:
    $ rustc main.rs
    
  4. Finally, we can execute the program:


    $ ./main
    
    Compiling and running our Hello World Rust script in Linux
    Compiling and running our Hello World Rust script in Linux

That’s all there is to it. You can now continue using Rust to build more complex projects.

Closing Thoughts

In this tutorial, we saw how to install Rust and use it to compile a Hello World program on a Linux system. These are the first steps to getting started with Rust, and you have already created your first program in the new language. From here, you can work on experimenting with the language and attempting to write more complex programs.



Comments and Discussions
Linux Forum