Hoi hoi! πΈ
Happy Friday! Let's learn a tiny bit of Rust! πΈπ»
No complicated architecture. No giant diagrams. Just a cat, a crab, and a little bit of ownership.
Grab a beer! πΊ
π Ownership::This Is My Cat!
Rust has three simple ownership rules:
Every value has an owner.
There can only be one owner at a time.
When the owner goes out of scope, the value is dropped.
Let's adopt a cat!
let a = String :: from ( "cat" ) ; let b = a ;
println! ( "{b}" ) ;
Enter fullscreen mode
Exit fullscreen mode
π¦ Move::Wait... Where's My Cat?!
What happens if we try to use a again?
println! ( "{a}" ) ; // β Compile error!
Enter fullscreen mode
Exit fullscreen mode
WHAT?! WHERE IS MY CAT?! π
Ownership moved from a to b .
Sorry, a . That's not your cat anymore. πΏ
Rust remembers. Rust never forgets. π¦
π€ Borrowing::Can I Pet Your Cat?
What if we just want to borrow a value without taking ownership?
We can use a reference ( & ).
let a = String :: from ( "cat" ) ; let b = & a ;
println!β¦