- implement tree
- linked list
- recursion
- async
For simple data which stored in stack rust make copy automatic because size is know on compile time
let s1 = 4
let s2 = s1
s1 and s2 stored in different stack, no need store that value in heap
let asd = 4;
let qwe = &asd;// qwe it is reference to asd, qwe not own the value of asd
let mut asd = 4;
let qwe = &mut asd;
Mutable reference restriction: if you have a mutable reference to a value, you can have no other references to that value. This code that attempts to create two mutable references to variable will fail.
let mut s = String::from("hello");
{
let r1 = &mut s;
} // r1 goes out of scope here, so we can make a new reference with no problems.
let r2 = &mut s;
also combining mutable and immutable reference cause error
- at any given time, you can have either one mutable reference or any number of immutable references.
- references must always be valid.
- data types - in general simple datatypes which stores in stack (i16,i32,str, etc...) and complext which most of the time stores in heap
- ownership - by default all data have owner - variable, on = clause we change the owner with futher consequences
- smart pointers
- Box - single owner, allow immutable or mutable borrows(checked at compile time)
- Rc - multiple owner to the same data, immutable, at compile time
- RefCell - single owner, allow mutate immutable data, at runtime time