String in Rust

in the core language, Rust provides only one type of string: str, which is often seen in its borrowed form &str. Rust’s standard library provdes another type: String. this type is growable, owned and mutable. creating new String let s1 = "some string".to_string(); // create string from literals let mut s = String::new(); // creates an empty string let s2 = String::from("initial content"); // initialize with a value // strings are utf-8 encoded.

Vector in Rust

Vector: stores value of same type stores values next to each other in heap can store values dynamically creating a Vector when using associate function ::new(), one must use type annotation so that compiler have info about what kind of values to be stored in there // empty vector let v1: Vec<i32> = Vec::new(); // vecor with some initial values let v2: vec![1, 2, 3]; updating a vector Vector provide methods for running operations on any of its instances.

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. A package can contain multiple binary crate and optionally one library crate. Additionally, a package can help encapsulate codes that are not meant for others to see. It can also enable scoping. Rust’s Module System: Packages: Cargo feature that let user build, test and share crates Crates: A tree of modules that produces a library or executable Modules and use: Let user contron the organization, scope and encapsulation.

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. Example: let some_var = Some(0u8); match some_var { Some(3) => println!("three"), _ => (), } can be written as: if let Some(3) = some_var { println!("three"); } if let is a syntax sugar for match that runs code when the value matches one pattern and then ignores all other values.

Match Control flow

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: enum Coin { Penny, Nickel, Dime, Quarter } fn match_coin(coin: Coin) -> u8 { match coin { Coin::Penny => 1, Coin::Nickel => 5, Coin::Dime => 10, Coin::Quarter => 25, } } fn main() { let c = match_coin(Coin::Penny); println!


enum is a type that can be any one of several variants. Enum Example enum IPAddrKind { V4, V6, } Enum Values Enum values can be accessed using :: which is useful. Reason is, each value inside that given enum is of same type. For example: enum IPAddrKind { V4, V6, } fn main() { let version_four = IPAddrKind::V4; let version_six = IPAddrKind::V6; // following are valid route(IPAddrKind::V4); route(IPAddrKind::V6); } // `ip_kind` would accept both `V4` and `v6` fn route(ip_kind: IPAddrKind) -> bool { true } Attaching data enum IPAddrKind { V4(String), V6(String), } fn main() { let home = IPAddrKind::V4(String::from("127.

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). A method(s) is defined using impl keyword. Method’s first parameter is always self, which represents the instance of the struct the method is being called on. Using Method Syntax struct Rect { height: i32, width: i32, } impl Rect { fn area(&self) -> i32 { self.width * self.


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. Defining a Struct Structures are defined using the keyword struct. Example: struct Person { name: String, age: u64, sex: karwa_do, } Instantiating Structs In order to create a new instance of an Structure, one has to call that structure with concrete values. Example:

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. Slicing an String let s = String::from("Hello, world"); let hello = &s[0..5]; If the initial range index is 0(i.e. first index), it could be omitted. If the last range index is 0(i.e. last index), it could be omitted as well.

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. fn main() { let s1 = String::from("Rust is awesome"); let length_s1 = calc_len(&s1); println!("The length of '{}' is {}.", s1, len); } fn calc_len(str: &String) -> usize { str.