One of Rust's biggest pros is its unique ownership system. Unfortunately, it is also one of the hardest things to learn. In this article I will try to explain it the same way I had learned it and how I introduce it to people.
Disclaimer: If you do not find this article helpful try to search for another. People are different and different things zing them.
Let's have a book#
Ownership becomes simple and natural if you just acknowledge it as an application of real world relationships. For example, imagine types in Rust as a kind of written note. We have different types of notes and based on that, each 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
Using this analogy let me try to introduce you, dear reader, to the amazing world of Rust's ownership.
One can own the book#
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 you are done with this book you will need to get rid of it. Since you are a law abiding citizen you will recycle the note in the appropriate receptacle, but it is your responsibility to do it. Of course this is not the only way to deal with a note. You can also give it to someone and then it will be hers or his responsibility.
To rephrase it in the Rust way, it would look like this:
; // at the end of his life john will destroy all his belongings
One can borrow the book#
When we don't want to give someone a book (we like that one), we can also lend 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.
- 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 make us go rampage and destroy the whole universe. It will be an immutable borrow.
Of course if we borrow something from someone else, then we can lend it further with the same rules that were applied to us.
Rust also ensures that mutable borrow is unique. There will never be more 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.
Immutable borrows aren't exclusive. I can lend my books as many times as I want with one exception: I cannot lend a book that is still borrowed by someone who can change its content.
In Rust it would look like that:
Not all notes are worth borrowing#
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 you are in school and friend wants to copy your homework. What you do is lend your homework to him, and with caution he can clone it on his own. This is what Rust's
Clone trait provides - a method to clone content of struct without moving its ownership.
But some notes are even shorter than that. They are so short and easy to clone that it is much easier to clone them every time, instead of explicitly calling the method. Like when you give your phone number to a hot girl at the bar, the
Copy trait automatically clones your note so the other has their own copy. Again, this is for small types that can be mechanically copied each time when needed.
// everything that is `Copy` must be also `Clone` ;
There is more to learn, but these are the basic laws of ownership in Rust. 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.