Rust 101 — Everything you need to know about Rust

DADAYNEWS MEDIA 36 3

Introduction to Rust Programming — the most loved programming language. Since 2014, I have learnt quite a few languages, mainly focusing on Java, Kotlin and JavaScript. Although I recently learnt Haskell for university purposes, I wanted to learn a new language that has a better overall reputation. A few weeks ago, I turned to Rust, and it has been really good so far. In this article, I am going to share the basics of Rust so that you can start programming today.

Why Rust?

Rust is a programming language that is built to be fast, secure, reliable and supposedly easier to program. Rust was, on the contrary, not built to replace C or C++. It’s aim is completely different, that is, to be an all-purpose general programming language.

Features of Rust

  1. Code should be able to run without crashing first time — Yes, you heard me. Rust is a statically typed language, and based on the way it is constructed, it leaves almost (I say, almost!) no room for your program to crash.
  2. Super fast — Rust is extremely fast when it comes to compilation and execution time.
  3. Zero-cost abstractions — All Rust code is compiled into Assembly using the LLVM back-end (also used by C). Programming features like Generics and Collections do not come with runtime cost; it will only be slower to compile. (Source: stackoverflow).
  4. Builds optimized code for Generics — If you have generic code, the Rust’s compiler can smartly identify the code for, say, Lists and Sets, and optimize it differently for both.
  5. Rust Compilation Errors — Rust’s compiler gives the most helpful error messages if you mess up your code. In some cases, the compiler will give you code that you can copy/paste in order for it to work.
  6. Documentation — Rust has an amazing Documentation, and a tutorial guide called The Rust Book. The Rust Book is your best friend in this journey.

Now let us get into actually programming in Rust.

Installing Rust On Your System

The Rust Book explains how you can install Rust on your system.

The Rust Programming Language

The first step is to install Rust. We’ll download Rust through rustup, a command line tool for managing Rust versions…

doc.rust-lang.org

Since I use Linux, it is a simple command that installs Rust without any hassle,

A Brief tour of Rust

Variables

By default, all variables are immutable unless we mark them. Rust is statically typed, so if a variable is declared to be mutable, we must be careful to assign it of the same type.

Defining variables are simple:

If we declare a variable, and we try to use it without initializing, the Rust compiler will complain.

As Rust programmers, we must read all error messages fully. The error message here tells us that we have used coins but we haven’t initialized it. The message goes on to say that in line 2, we should append = 0 in order for the code to work!

There are many data types used in Rust. We have come across Stringi32 and u32. We also have,

Those above were some more primitive data types. Rust has support for compound data types as well!

The implicit type for pair is (char, i32). Tuples are heterogeneous and can support nested tuples as well.

Additionally, we can work with Arrays as well,

A data type declaration can hint towards a quick way to initialize arrays.

Functions

We have seen we have been using the main function to denote the starting point in our program. The syntax of defining functions is

An example function can be like:

Notice I do not have a semicolon at the end of that statement. This signifies that the expression will return a particular value. If I add a semicolon, Rust will treat the expression as a statement and will complain I am not returning a boolean value.

Let Expressions

Combining the knowledge of variables and functions, we can assign values like this:

Hence, we can conclude that:

Variable Shadowing and Scopes

Notice how I didn’t prefix the previous code block with // * WILL NOT COMPILE. However, I do have two declarations of the same variable. This is called variable shadowing.

First we initialize x to be 0, and then I am re-initializing it to be 10. This is a valid program and useful in many ways when we couple it with scopes!

Scopes are just a block of code where shadowed variables do not affect the value of the variable outside the scope.

Namespaces

If we wish to use functions from other libraries, we can use namespaces.

We can also bring the function into scope by using the use keyword.

We can also use std::cmp::* to bring every function inside std::cmp into scope.

Structs

Structs are similar to structs in C. We can define a struct Coordinate as below and initialize a variable of that type.

We can also implement our functions to structs that we define.

Pattern Matching

Pattern Matching is like a conditional structure.

We can also perform pattern matching using the match construct.

Alternatively, this code also compiles:

.. here means ignore the (remaining) properties inside the Coordinate struct.

Traits

Traits are like type classes in Haskell, or interfaces in Java. Say that we have a struct, Number which looks like this:

We could define a trait Parity that contains a function is_even. If we implement Parity for Number, we need to define the trait’s functions.

We can also implement traits for foreign types as well!

But what we cannot do is define foreign traits for foreign structs

Macros

Macros are a part of meta-programming. Marcos can be considered as little programs that other programs can use. All macros end with ! and can be defined as either of the following:

println! is a macro that uses std::io to write something to the console. Similarly, vec![] defines a vector, which is an array with steroids.

panic! is also a macro that terminates a program (actually a Thread, if you know about concurrency) with an error message. panic! is one of the only places in our program that can crash.

Enums

Enums in Rust are like sealed class in Kotlin. They are certain types that we can define in an enclosure. The following example is coupled with Generics. Option is also defined in the standard library.

The Result enum

The most popular enum Rust provides us with is Result.

If we have a function that returns a result, we can safely handle it.

The reason I have used this code within a scope block is if we wish to propagate the error up somewhere, we could replace the code with:

There is a shorthand to do this operation:

Now if result returns an Ok(data), then the question mark will return the data object to us.

Leave a Reply

Your email address will not be published. Required fields are marked *

Home
Account
Community
Search