rust

Ownership

Ownership Rules 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. Type String can be mutated, but string literals can not. Reason behind: literals are stored in stack during compile-time, which makes it impossible to grow an string during runtime. Type String can be mutated because, it’s stored in heap.

Control Flow

if, if else, else if An if expression allows you to branch your code depending on conditions. fn main() { let num: i8 = 2; if num < 5 { println!("Number is Samll"); } else if num % 2 === 0 { println!("Divisible by 2"); } else { println!("Number is Big"); } } In Rust, condition must resolve to a value of type bool, i.e. either true or false. Otherwise the program won’t compile.

Functions

Functions are block of codes which are instructed to accomplish specific task(s). To write a function, the fn keyword is used, followed by function name. By convention, snake_case is used while naming a function. Parameters Functions can accepts parameters, which are special variables that are part of a function’s signature. Parameter names are followed by a type name. fn main() { another_function(32); } fn another_function(x: i32) { println!("{}", x); } Function Body Whatever resides inside the curly braces after function name, called as function body.

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. Data Types in Rust: Scalar Integers (i8 | u8) - 8-bit Integer (i16 | u16) - 16-bit Integer (i32 | u32) - 32-bit Integer (i64 | u64) - 64-bit Integer (i128 | u128) - 128-bit Integer (isize | usize) - Integer size depends upon the system architecture.

Variables and Mutablity

In Rust, Variables are Immutable by default. Demo Program Immutable // This program won't compile because // the value of `x` has been changed after // the first declaration(without using `mut`) fn main() { let x = 5; println!("Value of X is {}", x); x = 15; println!("Value of X is {}", x); } Output of cargo run: $ cargo run Compiling variables v0.1.0 (/home/crow/Dev/rust/sources/variables) error[E0384]: cannot assign twice to immutable variable `x` --> src/main.

Guessing Game

use rand::Rng; use std::cmp::Ordering; use std::io; fn main() { println!("Guess the Number!!"); loop { println!("Please input your guess"); let mut guess = String::new(); let secret_number = rand::thread_rng().gen_range(1, 200); io::stdin() .read_line(&mut guess) .expect("Faild to Read Line"); let guess: u32 = match guess.trim().parse() { Ok(num) => num, Err(_) => { println!("Invalid Input."); continue; } }; match guess.cmp(&secret_number) { Ordering::Less => println!("Too Small!"), Ordering::Greater => println!("Too Big!"), Ordering::Equal => { println!("You win!"); break; } } } } Explanation The use statement use rand::Rng; use std::cmp::Ordering; use std::io; Rust only brings a few types into global scope, by default.

Cargo

Cargo is the Rust package manager and build tool. It’s used to initialize, build and manage dependencies of a project. 1. cargo new PROJECT_NAME $ cargo new cargo-example Created binary (application) `cargo-example` package To initialize a project, one needs to run cargo new followed by the project_name. Cargo will create a directory project_name, add .gitignore, src/main.rs and Cargo.toml. The src/main.rs is the entry-point to the project. Rust expects project related codes to live inside src directory, leaving top-level directory for config files, readme etc.

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. 1. The fn Keyword fn is a reserved keyword in Rust. It’s used for declaring a function. 2. The main() function fn main() { } main() function is the entry point of any Rust program. Execution of codes start from here. main() function nerither accepts parameter, nor returns anything.