MENU

Intro to Rust

Rust is a statically typed language backed by Mozilla. Rust have no built-in garbage-collection mechanism, yet it gives the maximum possible security to memory leaks.

Cargo

Cargo is the Rust package manager and build tool. It’s used to initialize, build and manage dependencies of a project.

Guessing Game

```rs use rand::Rng; use std::cmp::Ordering; use std::io;

Variables and Mutablity

In Rust, Variables are Immutable by default.

Data Types

Every value in Rust is of a certain data type, which tells Rust what kind of data is being specified so it knows how to work with that data.

Functions

Functions are block of codes which are instructed to accomplish specific task(s).

Control Flow

if, if else, else if

Ownership

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

References and Borrowing

When a variable is passed by reference to a function parameter, the function does not take ownership of that variable. Instead, it stores a pointer which would refer to that specific memory location. Passing a variable by reference to a function is known as Borrowing.

Slice Type

Another data type that does not have ownership is the slice. Slices let you reference a contiguous sequence of elements in a collection rather than the whole collection.

Structures

Structures(struct) are compound(and user-defined) data types. Structures are much similar to tuples, except that data can named. Structures can be set as a return type in a function.

Structures: Method Syntax

Methods are smiilar to function. Unlike functions, methods are defined within the context of a structure(or an enum or a trait object).

Structures: Method Syntax

enum is a type that can be any one of several variants.

`Match` Control flow Operator

match is a control flow operator. It works similar to a coin sorting machine, i.e. it would start matching patterns(a.k.a arms) from top to bottom until it finds a matching pattern. Example:

Concise Control Flow with if-let

if let syntax allows to write if and let togather in a less verbose way to handle values that match one pattern while ignoring the rest.

Package Management

Package management comes into play when a project gets bigger and bigger. Dividing codes into multiple files and grouping them make a project more maintainable.