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