Advent of Code

Advent of Code 2022

After my placements, I decided to pick up rust.

—write benefits

Ownership Model

  • Pros:
    • Control over memory
    • Error free
    • Faster runtime
    • Smaller program size
  • Cons:
    • Slower write time.
    • Steeper learning curve.

Ownership rules

  • Each variable has its own owner.
  • Each variable has only 1 owner at time.
  • When owner is out of scope, value is dropped.

– stack and heap memory

let s1: String = String::from("hello");
let s2: String = s1;
//here neither a deep copy
// nor shallow copy is created
//it moves it to 2nd variable

//for cloning
let s2 = s1.clone();

Passing string without reference transfers ownership. Passing with reference doesent change ownership. -> this called borrowing. references are immutable by default.

for making them mutable

fn main()
    let mut ss: String = String::from("hello")
    change(&mut s1);
fn change(ss: &mut String)
    ss.push_str(string: ", world.")

We can call only one mutable only once at a time.

Once we have a immutable ref, we cant have a mutable ref.

But we can have multiple immutable reference.

You can declare a new variable with the same name as a previous variable, here we can say **the first one is shadowed by the second one.

With slices we can reference elements of a collection. Structs and implementations