Hello World in Rust

After reading the paper from Google on Memory Safety, I decided to give Rust a try and do a simple Hello World: https://research.google/pubs/secure-by-design-googles-perspective-on-memory-safety/

Installation

It’s very simple, I installed Rust in Ubuntu by following this tutorial : How To Install Rust on Ubuntu 20.04 | DigitalOcean

The command to install is:

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

After installing it, using Nano I created a testrust.rs file with the following contents:

fn main()
{
println!("Congratulations! Your Rust program works.");
}

Then, compiled it with the following command:

rustc test.rs -o testrs

To execute the binary:

./testrs

Congratulations! Your Rust program works!

Very easy!

Hello world in C++

I created a simple Hello World in C++ with the following contents:

#include <iostream>

int main()

{

std::cout << "Congratulations! Your C++ program works." << std::endl;

return 0;

}

Compiled the file with the following command:

g++ test.cpp -o testcpp

Simplified Comparison of C++ vs Rust

I decided to compare both in terms of source code size and binary size

ls -l

total 3728

-rw-rw-r-- 1 119 Mar 9 23:43 test.cpp

-rwxrwxr-x 1 17320 Mar 9 23:43 testcpp

-rw-rw-r-- 1 71 Mar 9 22:45 test.rs

-rwxrwxr-x 1 3785312 Mar 9 22:47 testrs

Source Code Size

The C++ File is 119 bytes where as the Rust file is only 71 bytes

C++ seems to be more verbose

Binary size

The C++ generated binary is only 17,320 bytes whereas the Rust binary is 3,785,312 bytes.

The Rust binary is significantly bigger