Rust has become one of the most loved programming languages in the last few years due to its drastic features related to memory management and low-level control. Due to its rich features it has become first language after C to be used for Linux kernel development. In this tutorial, we will see how to install rust in Ubuntu Linux and compile your first hello world program here. So, let’s get started!
Install Rust in Ubuntu:
Open the terminal and run the following commands to install Rust in your Ubuntu/Linux machine.
sudo apt install build-essential
sudo apt update && sudo apt upgrade
curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env
rustc --version
sudo apt update && sudo apt upgrade
Hello World in Rust:
Now the rust is installed in your ubuntu or any other Debian machine you are using right now. Its time to write our first hello world rust code now.
Make a hello.rs file at any location you want. The rust source code files uses .rs extension. For this tutorial, we will make a hello.rs file in hello-world folder at home
mkdir ~/hello-world # make hello world directory at home
cd ~/hello-world # change to hello world directory at home
touch hello.rs # create a rust program file with .rs extension
Open the file and paste the following code there in hello.rs file
fn main() {
println!("Hello, World!");
}
The rust uses rustc compiler to compile the code like c uses gcc and c++ uses g++ for compilation. Compile the hello.rs file using the following command.
rustc hello.rs
If the program compiles successfully, it will generate a binary file named as hello in the same directory. Run that file and you will see a hello world printed in your terminal as shown below
./hello
Boom! You have successfully compiled and run your first hello world program in rust on linux/ubuntu machine. Thats all from today’s tutorial. Have a look at the guidelines to compile Rust application in Yocto.