Rust Programming Language

February 11, 2018

Harry Gill
Tags: rust learning | 2min Read

Image credit: http://rust-lang.org/logos/rust-logo-512x512.png

Few years back I took CS50 certificate course form Harvard University, that sparked my interest in compiled programming languages. The course mainly used C Language to teach many programming concepts.

The course is available here if you are interested CS50’s Introduction to Computer Science.

Recently I have started learning rust programming language.

rust is compiled statically typed, safe language that aims to catch many bugs at compile time, according to their website:

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.

So far I have been learning the basics of rust and it has been a great experience. I like the fact that the language takes very good reasonable defaults that are quite unique. i.e. all variables are immutable by default you will have to use mut keyword if you need it to change in future

let name = "Unison";
name = "Unite"; // not allowed compile time error
let mut industry = "childcare";
industry = "Hospitality"; // fine to do

One of the unique and most powerful feature of rust is its memory management by Ownership system. In short, there are three rules of rust Ownership.

  1. Each value in Rust has a variable that is called its owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value will be dropped.

for example

{
  let v1 = String::from("hello"); // allocates memory on heap under the hood
  let v2 = v1; // s1 no longer own the string, ownership has moved to s2

  println!("{}, world!", v1); // compiler error here
} // v2 is dropped here and memory freed

println!("{}, world!", v2); // v2 is not valid anymore

rust takes a radically different approach than C when it comes to variables, pointers and memory management. More abut ownership and moves will be discussed in another post. In the mean time I will try to redo all the CS50 problem sets in rust instead of C.