Rust
Notion
mut, all variables areconst, to make itmodifiableadded themutResultof a.method_name()is anenum.- We call each possible state of
Resultavariant. Result'svariantsfor.read_line()areOkandErr.- The blocks of code after
if: called arms{} let mut s="jjj"not allowed,let mut s=String::from("jjj")allowed using the heap mémoirelet s2 = s1:s1movetos2, thens1will be droped
re-use Var: shadows
#![allow(unused)] fn main() { let x=10; let x="ll"; }
mutnot allowing the shadows
#![allow(unused)] fn main() { let mut x=10 x="ll"; }
variables
#![allow(unused)] fn main() { let x : i32= 4; // u32,u8.. let x : i32= 4_000; // 4_000 == 4000 let y: f32 = 3.0; // defualt is f64 let floored = 2 / 3; // Results in 0 let t: bool = false; let heart_eyed_cat: char = '😻';// 'z' ...should use single for char }
Tuple
#![allow(unused)] fn main() { let tup:(i32, f64, u8) = (500, 6.4, 1.5); let (x, y, z) = tup; println!("The value of z is: {z} or {} or {tup.2} or {}",tup.2); // {tup.2} -> not allowed }
Arr
Unlike a tuple, every element of an array must have the same type. Unlike arrays in some other languages, arrays in Rust have a fixed length.
#![allow(unused)] fn main() { let a = [1, 2, 3, 4, 5]; let first = a[0]; }
What Is Ownership?
Memory is managed through a system of ownership with a set of rules that the compiler checks. If any of the rules are violated, the program won’t compile. None of the features of ownership will slow down your program while it’s running.
- Ownership Rules
- Each value in Rust has an owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped.
- the error
double free erroris solved byshallow copy
shallow copy: not drop the heap data of var if this data shared between
another var
deep copy,s1ands2will be separated
#![allow(unused)] fn main() { let s1 = String::from("hello"); let s2 = s1.clone(); }
- types that used head memory used move and other used copy function
#![allow(unused)] fn main() { let s = String::from("hello"); takes_ownership(s); s:String("new"); // not allowed because moved from this scope }