My personal blog hauleth.dev
blog
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1 from trosel/patch-1

edit for grammar, clarify Copy section a bit

authored by

Łukasz Jan Niemier and committed by
GitHub
d0fd68e7 7a604c25

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