···
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.
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.
People are different and different things *zing* them.
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.
- short ones, like phone no. of the hot waiter/waitress
- longer ones, like this article
- longest ones, like a *Lord of the Rings*
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
world of Rust's ownership.
···
Each note, no matter what size it is, can have one owner. Me, you, anyone, it
doesn't matter, but there will be only one owner. You can do whatever you want
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.
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:
50
-
let book = Note; // he creates the book and he owns it
50
+
let book = Note; // john creates the book and he owns it
// 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
55
-
fn someone_with_testament() {
let book = Note; // new book
// he can do whatever he wants to do with his book
61
-
// he gives her book to `someone_else`,
62
-
// altogether with responsibility to destroy it
61
+
// steve gives the book to `sally`,
62
+
// Sally has the responsibility to destroy it
64
-
// now he cannot do anything with this book,
64
+
// now steve cannot do anything with this book,
// as it is not his personal belonging anymore
## One can borrow the book
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:
- We can edit that book (ex. it is our personal dairy) and we lend it to someone
to check our spelling. We trust that person and we explicitly allow her to
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
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
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.
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
of trust - like when I find someone who is better at English than me, I would
allow this person to correct an article written by me or my friend who has
entrusted me with correcting his text.
···
spelling_corrector(&mut book);
// we must explicitly mention that we lend the book
103
-
// and we not give it away
103
+
// and we don't give it away
···
Sometimes this whole process of lending and then receiving a note back is much
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
···
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.
150
-
// everything what is `Copy` must be also `Clone`
150
+
// everything that is `Copy` must be also `Clone`
···
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
easier for you to understand how other types behave and, more importantly, why
they work the way they do.