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