···1010 - borrowing
1111---
12121313-One of the biggest Rust's pros is unique ownership system. Unfortunately, it is
1414-also one of the hardest thing to learn. In this article I will try to explain
1515-it the same way I had learnt it and how I introduce people to one.
1313+One of Rust's biggest pros is its unique ownership system. Unfortunately, it is
1414+also one of the hardest things to learn. In this article I will try to explain
1515+it the same way I had learned it and how I introduce it to people.
16161717-**Disclaimer**: If you do not find this article helpful try to search another.
1717+**Disclaimer**: If you do not find this article helpful try to search for another.
1818People are different and different things *zing* them.
19192020## Let's have a book
21212222Ownership becomes simple and natural if you just acknowledge it as an
2323-application of real world relationship. For example imagine any type in Rust as
2424-a form of text note. We have different type of notes and basing on that, each
2525-of them we will handle differently.
2323+application of real world relationships. For example, imagine types in Rust as
2424+a kind of written note. We have different types of notes and based on that, each
2525+of them will be handled differently.
26262727- short ones, like phone no. of the hot waiter/waitress
2828- longer ones, like this article
2929- longest ones, like a *Lord of the Rings*
30303131-Using this analogy let my try to introduce you, dear reader, to the amazing
3131+Using this analogy let me try to introduce you, dear reader, to the amazing
3232world of Rust's ownership.
33333434## One can own the book
···3636Each note, no matter what size it is, can have one owner. Me, you, anyone, it
3737doesn't matter, but there will be only one owner. You can do whatever you want
3838with such note but with that power comes, not so great, responsibility: after
3939-you are done with this book you will need to destroy it. As you are a law
4040-abiding citizen you will recycle it in appreciate institution, but it is your
4141-responsibility to do it. Of course it is not the only way to deal with book. You
4242-can also give it to someone and then it will be her or his responsibility.
3939+you are done with this book you will need to get rid of it. Since you are a law
4040+abiding citizen you will recycle the note in the appropriate receptacle, but it is your
4141+responsibility to do it. Of course this is not the only way to deal with a note. You
4242+can also give it to someone and then it will be hers or his responsibility.
43434444-To rephrase it in Rust way, it would look like that:
4444+To rephrase it in the Rust way, it would look like this:
45454646```rust
4747struct Note;
48484949-fn someone() {
5050- let book = Note; // he creates the book and he owns it
4949+fn john() {
5050+ let book = Note; // john creates the book and he owns it
51515252 // here he can do whatever he want with our `book`
5353-} // at the end of his life he'll destroy all his belongings
5353+} // at the end of his life john will destroy all his belongings
54545555-fn someone_with_testament() {
5555+fn steve() {
5656 let book = Note; // new book
57575858 // he can do whatever he wants to do with his book
59596060- someone_else(book);
6161- // he gives her book to `someone_else`,
6262- // altogether with responsibility to destroy it
6060+ sally(book);
6161+ // steve gives the book to `sally`,
6262+ // Sally has the responsibility to destroy it
63636464- // now he cannot do anything with this book,
6464+ // now steve cannot do anything with this book,
6565 // as it is not his personal belonging anymore
6666}
6767```
68686969## One can borrow the book
70707171-When we don't want to give someone a book (we like that one) we can also lend
7272-him one. And there are two ways to borrow one book:
7171+When we don't want to give someone a book (we like that one), we can also lend
7272+them one. And there are two ways to borrow one book:
73737474- We can edit that book (ex. it is our personal dairy) and we lend it to someone
7575 to check our spelling. We trust that person and we explicitly allow her to
7676 edit our notes in place. We call it **mutable borrow**.
7777-- We do not trust one and we lend our beloved book with no permission to edit
7777+- We do not trust someone and we lend our beloved book with no permission to edit
7878 it. Even more, that person knows, that writing something in that book will
7979- make us go rampage and destroy whole universe. It will be **immutable
7979+ make us go rampage and destroy the whole universe. It will be an **immutable
8080 borrow**.
81818282Of course if we borrow something from someone else, then we can lend it further
8383-on the same rules that was applied to us.
8383+with the same rules that were applied to us.
84848585Rust also ensures that **mutable borrow** is unique. There will never be more
8686-than one person that will be allowed to edit the book. We still can create chain
8686+than one person that will be allowed to edit the book. We can still create a chain
8787of trust - like when I find someone who is better at English than me, I would
8888allow this person to correct an article written by me or my friend who has
8989entrusted me with correcting his text.
···100100101101 spelling_corrector(&mut book);
102102 // we must explicitly mention that we lend the book
103103- // and we not give it away
103103+ // and we don't give it away
104104105105 reader(&book);
106106}
···118118119119Sometimes this whole process of lending and then receiving a note back is much
120120more complicated then just cloning the whole note for someone else. Imagine that
121121-you are in school and friend wants to copy your homework. What you do is lending
122122-your homework to him, with caution he can clone it on his own. This is what
123123-Rust's `Clone` trait provides - method to clone content of struct without moving
121121+you are in school and friend wants to copy your homework. What you do is lend
122122+your homework to him, and with caution he can clone it on his own. This is what
123123+Rust's `Clone` trait provides - a method to clone content of struct without moving
124124its ownership.
125125126126```rust
···139139}
140140```
141141142142-But some notes are even shorter than that. They are so short and easy to copy
143143-that it is much easier to copy them every time you need, instead of explicitly
144144-mentioning that we are making a copy - like when you give your phone number to a
145145-hot girl at the bar. This is `Copy`. Small type that can be mechanically copied
146146-each time when needed.
142142+But some notes are even shorter than that. They are so short and easy to clone
143143+that it is much easier to clone them every time, instead of explicitly
144144+calling the method. Like when you give your phone number to a hot girl at the
145145+bar, the `Copy` trait automatically clones your note so the other has their own copy.
146146+Again, this is for small types that can be mechanically copied each time when needed.
147147148148```rust
149149#[derive(Copy, Clone)]
150150-// everything what is `Copy` must be also `Clone`
150150+// everything that is `Copy` must be also `Clone`
151151struct PhoneNo;
152152153153fn my() {
···163163164164## Conclusion
165165166166-There is more stuff to learn, but these are the basic laws of ownership in Rust.
167167-Everything else is based on those and if you learn that, it will became much
166166+There is more to learn, but these are the basic laws of ownership in Rust.
167167+Everything else is based on this. If you understand this, it will become much
168168easier for you to understand how other types behave and, more importantly, why
169169they work the way they do.