Ade Malsasa Akbar contact
Senior author, Open Source enthusiast.
Sunday, October 30, 2022 at 00:17

You might want to learn Rust on Ubuntu. Rust is a new computer programming language in the same categories as older ones namely C, C++ and Java. Using it, one can make programs for desktop, laptop, web and server as well as embedded computers. Created by Mozilla in 2010, Rust is now growing to be used to develop many critical software including some you use everyday, such as Firefox's Quantum engine, a FOSS remote desktop called RustDesk, and a new operating system called Redox OS. This tutorial will help you install required tools and write code in Rust. Don't worry to exercise as we also include uninstall steps too. What are you waiting for? Now let's try Rust for sure. 


Subscribe to UbuntuBuzz Telegram Channel to get article updates.

A. Install Required Software


At the moment we write, Ubuntu 22.04 and 22.10 do not ship Rust compiler yet but only do for text editors that support Rust. In short, in this exercise you will need Rust compiler and Geany text editor. Follow these instructions:

Install Text Editor:

$ sudo apt-get install geany
$ geany --version
$ geany

Install Compiler:

$ sudo apt-get install curl 
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source "$HOME/.cargo/env"
$ rustc --version
$ cargo --version
$ rustup --version

Uninstallation:

If you want to uninstall Rust compiler installation completely from Ubuntu, follow these instructions:

$ rustup self uninstall
(Answer the question with Y)
 

B. Write a Program


A program in Rust language is a text file with .rs extension. You can compare this to C with .c, C++ with .cpp and Java with .java. A Rust binary executable file extension will depend on the operating system, for example, .exe on Windows or .app on MacOS or .elf on GNU/Linux. Please note that in Unix family systems, program files may also have no extension at all. Because of that, we will use .elf extension for executable and .rs for source code to make the exercise easier for beginners.

1. Run Geany
2. Create a new file
3. Save it as program.rs in home directory
4. Write code below
5. Save

fn main(){
println!("Hello, world!");
}


C. Compile and Run the Program

To be meaningful to computer, every time we write a Rust program, we must compile it thus computer can run it. That means, translated in a way the source code changed into binary code suitable for your computer and operating system versions. There are 2 ways to do this, using CLI and using GUI.

Using command lines:
$ rustc -o program.elf program.rs 
$ ./program.elf
  

At the above, you compile the source code file program.rs into binary executable file program.elf and finally execute the resulting executable file. As a result, you see Hello, world! message on your screen. It means your program works successfully.

Using graphical text editor:
1. Run Geany
2. Open program.rs
3. Click Compile
4. Click Build
5. Click Run
 

At the above, you do essentially the same thing as with the CLI method previously. You compile, run, and see the resulting Hello, world! message on your screen. What's the difference? Using this GUI method you use mouse clicks rather than command lines as Geany, the text editor, do it for you. 

Now you should understand how to do both methods.

D. Editing and Repeating

Finally, read Rust examples on their website (link available below). Try to edit your first code, compile it, and run it. Next, try again to write new code, save it, compile it, run it, and repeat. Feel free to use Geany, the text editor, as it is very convenience and fast to use for beginners. For example, we tried 10 examples one by one resulting in ten source files program-01.rs to program-010.rs. See picture below. 


Happy hacking!

E. Further Reading


The Rust Programming Language Book by Rust Project. 

Rust By Example, sample codes you can run, study, modify and share.

Rust (Programming Language) on Wikipedia


This article is licensed under CC BY-SA 3.0.