Taking another break from OS study and will be learning another programming language and that will be Rust which is a "blazingly fast and memory-efficient: with no runtime or garbage collector, it can power performance-critical services, run on embedded devices, and easily integrate with other languages". But first of all:
Why Learn This Language?
The primary reason I can think of learning this language is beasue of WebAssembly. Now what is a WebAssembly? WebAssembly is a binary-code that allows a user to execute code written in languages other than JavaScript on the web at near native speed. Rust is one of the languages that can be compiled to WebAssembly and used to build high performance apps on the web.
Other reasons for learning this language is that it guarantees memory safety, easy to learn (for those who already know C or C++) and is compatible with all operating systems. Now we will begin learning Rust but first we need do have to take care of some initial setup.
Initial Setup
To install the Rust compiler on Windows Machine, Go to Rust's website and download the Rust compiler .exe file and install it as any other software. And if you are on UNIX based operating systems it is much easy you just need to run the following command.
Linux>> curl --proto '=https' --tlsv1.2 -sSf https://sh.Rustup.rs | sh
After installation you can type the following command in the shell to be sure that the installation was successful.
C:\Users\User> rustc --version
Rustc 1.51.0 (2fd73fabe 2021-03-23)
After that we will be needing a code editor for writing our Rust code and for that I will be using Visual Studio Code.
Writing First Rust Program
Every Rust file ends with .rs
just like how python file ends with .py
. Now first I created a directory named Rust and then created a file called main.rs
in it and opened it in VS-Code.
Linux>> mkdir Rust
Linux>> touch main.rs
Linux>> code .
This is what a simple Rust program looks like. There is a main function just like C and C++ that is the entry point to your program.
fn main() {
println!("Hello World!");
}
To run this, first we have to compile it and to compile a Rust program there are two ways, either use Rustc
or cargo
. For a small program we can simply use Rustc
. cargo
is used to compile large projects.
Linux>> rustc main.rs
./main
Linux>> ./main
Hello World!
And that it for today and in the next post we will be learning about variables, comments, functions and all other basics of Rust programming language.