Rust off the grid: Introduction to Rust Programming Language.

Rust is a systems programming language focusing on safety, speed, and concurrency. It accomplishes these goals by being memory safe without using Garbage Collection.

Rust is a very modern language. It uses LLVM on its backend. Rust supports a mixture of imperative procedural, concurrent actor, object-oriented and pure functional styles. It also helps generic programming and meta programming, in both static and dynamic techniques.

Installation

If you’re on Linux or a Mac, all you need to do to install Rust is power up your terminal and run :

$ curl -sf -L https://static.rust-lang.org/rustup.sh | sh

If you’re on Windows, please download either the 32-bit installer or the 64-bit installer and run it.

If you’ve got Rust installed, you can open up yourshell, and type this:

$ rustc — version
rustc 1.20.0

Which prints the running rust version.

Here is the official installation guide.

Getting Started !!

Say hello to Rust .

Now let’s get started with our traditional program, “Hello World “
Our program begins with the main function, statements after the main function will be executed while complied.

println! is called as a macro which is similar to a function but ends with an exclamation/bang “!”, but instead of a function call, macros are expanded into source code that gets compiled with the rest of program.

Now to run our program first, we need to compile our code using the command ‘rustc file_name.rs’ which then generates a binary file which should to executed.

$rustc hello_rust.rs
$./hello_rust
Hello World!

Comments

As we know every programming language needs comments, Rust supports a few different varieties of comments, which are ignored by the compiler.

These are the types of comments which we often use.

// — Single line comments which go the end of the line.
/* */ — Multiple line comments which go to the closing delimiter.

Variables

A variable is a piece of storage that contains a value. In Rust, it’s easy to define a variable and set a value to it. Imagine you want to store the number 1in a variable called one.

let one = 1

As simple as that! You just assigned the variable one to the value 1, let is used to introduce a binding.

Type Annotation

Rust offers you to declare variables of your own size, as rust is a statically typed language we can specify our types up front, and they’re checked at compile time.

Here is an example of declaring 32-bit signed integer. We use let for binding, followed by a variable name and the type, size which come after a colon (:).

let x: i32 = 19;

Rust has many different primitive integer types. They begin with i for signed integers and u for unsigned integers. The possible integer sizes are 8, 16, 32, and 64 bits.

Mutability

In Rust when you declare a variable, the bindings are immutable which means we cant change the value of the variable.

This piece of code gives an error it shows re-assignment of immutable variable `x’!

$rustc rust_mut.rs
./rust_mut
--> rust_mut.rs:3:2
|
2 | let x = 5;
| - first assignment to `x`
3 | x = 10;
| ^^^^^^ re-assignment of immutable variable

If you want a binding to be mutable, you can use keyword mut.

Data Types

Primitive Data Types in Rust

1. Boolean

A standard boolean. Can be either true or false.

2. Characters

So far we’ve only worked with bools and numbers, but Rust supports letters too. Rust’s char type is the language’s most primitive alphabetic type, and the following code shows one way to use it:

3. Numeric Types

Integers

These types include i8, i16, i32, i64, isize, u8, u16, u32, u64, usize. The letter denotes whether it is signed (i) or unsigned (u), and the number denotes the size of the integer. So the type i8 is an 8 bit, integer and a u64 is an unsigned, 64 bit integer. isize and usize are dependent upon the architecture of the computer.

Floats

These types include f32 and f64. A floating point number is what we typically refer to as a decimal.

4. Arrays

An array is fixed-size, collection of same-type elements.

It is declared like:

let name: [type; size] = [elem1, elem2, elem3, elem4];

5. Tuples

Fixed-size ordered list of elements of different(or same) data types

Declaration of tuples is similar to an array. Instead we use “( )” instead of “[ ]”.

Tuples are also immutable by default, and even with mut, its element count cannot be changed. Also if you want to change an element’s value, the new value should have the same data type of previous value.

6. Strings

An str is a “String Slice,” and is the most primitive string type.

Now something that trips some people when getting started with rust is printing variables ‘{ }’. In order to print this we would need to write:

Which is similar to Python formatting.

7. Vectors

In Rust, vectors store their contents as contiguous arrays of T on the heap. This means that they must be able to know the size of T at compile time (that is, how many bytes are needed to store a T?).

You can create them with the vec! macro:

Control Flow

We always need the ability to check the conditions and change the behavior of the program accordingly. The conditional statements give us the ability; the purest form is the ‘if’ statement.

The most common constructs that let you control the flow of execution of Rust code are if expressions and loops.

If-Else

All if expressions start with the keyword if, which is followed by a condition. we can also include an else expression, to give the program an alternative block of code to execute should the condition evaluate to false.

Multiple conditions with else-if

Loops

Rust currently provides three approaches to performing some kind of iterative activity. They are: for, while and loop. Each approach has its own set of uses.

For Loop

For loops in rust is used for iterating based on the given start position and end position. Rust’s for loop doesn’t look like this “C-style” for loop, it looks like this.

In this example the variable x iterates from 0 to 9 and prints the numbers upto 9.

While Loop

Rust also has a while loop. While the condition is true, the loop runs. When the condition ceases to be true, you call break, stopping the loop.

It looks like this :

Loop

The loop keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.To stop this infinite loop we need to interrupt it by a keyboard shortcut (CRTL + C).

Functions

Function is a block of reusable code that performs a specific task. It is a small unit of computation which may take arguments and may return values.

Every Rust program has at least one function, the main function:

fn main() {
//body
}

In Rust to declare functions we use fn keyword followed by the function name, some parentheses where we include the arguments if we have any.

Now let’s write a function where we can add two integers.

The functions are declared called in the main function and declared out of it.

You separate arguments with a comma, both when you call the function, as well as when you declare it.

Functions with return type

By default functions return empty tuple (). If you want to return a value, return type must be specified after -> .

Here’s an example of a function that returns the numbers.

In the above program the function plus_one() and plus_two() returns the integer types and we are assigning them to the variables in the main function.


Congratulations you have just completed your basic Rust Programming !

Kudos to everyone who read the story and supported it.

Thanks for reading. If you found this story helpful, please click the below 👏 to spread the love.

Source code at:

References :