this repo has no description
1+++ 2date = 2019-07-14T17:38:48+01:00 3description = "Simple introduction to Rust's ownership system" 4title = "Here be (owned) books" 5 6[taxonomies] 7tags = [ 8 "rust" 9] 10+++ 11 12One of Rust's biggest pros is its unique ownership system. Unfortunately, it is 13also one of the hardest things to learn. In this article I will try to explain 14it the same way I had learned it and how I introduce it to people. 15 16**Disclaimer**: If you do not find this article helpful try to search for another. 17People are different and different things *zing* them. 18 19## Let's have a book 20 21Ownership becomes simple and natural if you just acknowledge it as an 22application of real world relationships. For example, imagine types in Rust as 23a kind of written note. We have different types of notes and based on that, each 24of them will be handled differently. 25 26- short ones, like phone no. of the hot waiter/waitress 27- longer ones, like this article 28- longest ones, like a *Lord of the Rings* 29 30Using this analogy let me try to introduce you, dear reader, to the amazing 31world of Rust's ownership. 32 33## One can own the book 34 35Each note, no matter what size it is, can have one owner. Me, you, anyone, it 36doesn't matter, but there will be only one owner. You can do whatever you want 37with such note but with that power comes, not so great, responsibility: after 38you are done with this book you will need to get rid of it. Since you are a law 39abiding citizen you will recycle the note in the appropriate receptacle, but it is your 40responsibility to do it. Of course this is not the only way to deal with a note. You 41can also give it to someone and then it will be hers or his responsibility. 42 43To rephrase it in the Rust way, it would look like this: 44 45```rust 46struct Note; 47 48fn john() { 49 let book = Note; // john creates the book and he owns it 50 51 // here he can do whatever he want with our `book` 52} // at the end of his life john will destroy all his belongings 53 54fn steve() { 55 let book = Note; // new book 56 57 // he can do whatever he wants to do with his book 58 59 sally(book); 60 // steve gives the book to `sally`, 61 // Sally has the responsibility to destroy it 62 63 // now steve cannot do anything with this book, 64 // as it is not his personal belonging anymore 65} 66``` 67 68## One can borrow the book 69 70When we don't want to give someone a book (we like that one), we can also lend 71them one. And there are two ways to borrow one book: 72 73- We can edit that book (ex. it is our personal dairy) and we lend it to someone 74 to check our spelling. We trust that person and we explicitly allow her to 75 edit our notes in place. We call it **mutable borrow**. 76- We do not trust someone and we lend our beloved book with no permission to edit 77 it. Even more, that person knows, that writing something in that book will 78 make us go rampage and destroy the whole universe. It will be an **immutable 79 borrow**. 80 81Of course if we borrow something from someone else, then we can lend it further 82with the same rules that were applied to us. 83 84Rust also ensures that **mutable borrow** is unique. There will never be more 85than one person that will be allowed to edit the book. We can still create a chain 86of trust - like when I find someone who is better at English than me, I would 87allow this person to correct an article written by me or my friend who has 88entrusted me with correcting his text. 89 90**Immutable borrows** aren't exclusive. I can lend my books as many times as I 91want with one exception: I cannot lend a book that is still borrowed by someone 92who can change its content. 93 94In Rust it would look like that: 95 96```rust 97fn my() { 98 let mut book = Note; 99 100 spelling_corrector(&mut book); 101 // we must explicitly mention that we lend the book 102 // and we don't give it away 103 104 reader(&book); 105} 106 107fn spelling_corrector(book: &mut Note) { 108 // correct spelling in place 109} 110 111fn reader(book: &Note) { 112 // read a book 113} 114``` 115 116## Not all notes are worth borrowing 117 118Sometimes this whole process of lending and then receiving a note back is much 119more complicated then just cloning the whole note for someone else. Imagine that 120you are in school and friend wants to copy your homework. What you do is lend 121your homework to him, and with caution he can clone it on his own. This is what 122Rust's `Clone` trait provides - a method to clone content of struct without moving 123its ownership. 124 125```rust 126#[derive(Clone)] 127struct Homework; 128 129fn my() { 130 let homework = Homework; 131 132 friend(&homework); 133} 134 135fn friend(work: &Homework) { // we lend it immutably 136 let mut homework: Homework = work.clone(); 137 // your friend now has his own modifiable copy 138} 139``` 140 141But some notes are even shorter than that. They are so short and easy to clone 142that it is much easier to clone them every time, instead of explicitly 143calling the method. Like when you give your phone number to a hot girl at the 144bar, the `Copy` trait automatically clones your note so the other has their own copy. 145Again, this is for small types that can be mechanically copied each time when needed. 146 147```rust 148#[derive(Copy, Clone)] 149// everything that is `Copy` must be also `Clone` 150struct PhoneNo; 151 152fn my() { 153 let no = PhoneNo; 154 155 hot_stuff(no); 156} 157 158fn hot_stuff(no: PhoneNo) { 159 // fingers crossed 160} 161``` 162 163## Conclusion 164 165There is more to learn, but these are the basic laws of ownership in Rust. 166Everything else is based on this. If you understand this, it will become much 167easier for you to understand how other types behave and, more importantly, why 168they work the way they do.