Rust - Definitive guide to ownership / Moving / Borrowing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's talk about rust and ownership and about essentially the rules of ownership so let's start first part moving because we have two concepts right we have moving and we're borrowing and borrowing the half-light traditional boring as a reed boring and we immutable borrowing so but first let's talk about moving right so moving as following rules Senshi right so we can say the following each resource as a variable both its owner this number one second is there can only be one owner at a time and the third is when an owner goes out of scope the resource is destroyed very simple very compact so one two and three so this is a very simple idea yeah but very powerful in a combination with aesthetic Bottega and maybe you could say that this is the heart of the rust language right I want to add some other conclusions which are not rules but more like conclusions right now welcome to visitors see when one scope transfers the ownership to another scope right then the previous owner is essentially irrelevant so we can say transfer of ownership makes the original owner irrelevant so let's check if this all works out right let's feed isn't code so we will make a function lender yeah and this function lender will lend variables out right so or essentially we'll do some stuff on valuables so let's make a variable called V which is a box the value of 220 as it into this 32 right and we will call a function that takes ownership ownership of this box value B yeah this is fine to take ownership function take ownership and we say a fee and we say ok this is a box of an integer let it do okay very simple and the only thing we do here is print line and we print the value inside the box okay so we get a value of 20 yeah it's exactly what about what we want so now let's check against the rules each resource has a variable called Zoners so first of all this was the owner right fee is owned until this point right now we call the function take ownership which has a signature which doesn't indicate any borrowing it's like a normal signature so now that V disappears essentially into disco and what we put they also set the transcript to ownerships makes the original owner irrelevant so that means that disco now has no ownership so we can conclude the following that V is owned here yeah but then we can say that V is not owned here anymore so how can we test this well very simple right let's do something with V yes so let's make this mutable so what's important if you want to change it so now I can say with V let's say the value of V is 40 right and let's execute this and we say yes perfect it works but now comes the kicker right so what if we would make here the values 60 so just following these rules after the ownership was transferred here right this should no error right so the static checker the compile time checker of Rosco's bar for this let's see and yes perfect we see here a perfect error message which says okay you move the value here into V yeah you took ownership here right so the value moved essentially in the scope of this function and was not returned with anything and so the value was used afterwards and this is an invalid operation so we actually don't allow you to compile so this is the most simple form of ownership okay all right so now let's look at borrowing and let's do this this one difference and so we are going to make a function called borrow and just to assert a minor so boring is different from essentially moving right so we because we have two different kinds we have borrowing that we borrowing and immutable body and we're first want to focus on repowering because that's the most simple form so let's define this function and borrow yeah it again takes a V and can takes a box with an integer 32 the main difference here right now is the following right instead of V we place essentially before the type signature and ampersand and this indicates that we're not moving the value but we to bother them so then again we do a print line and we essentially print the V right very simple function so now we need we need a V as well let's just define it that V is a new box only with a variable called 20 which is 32 and we're gonna Bob words so also here because we have to indicate we're borrowing the value so we need to place an ampersand before it so let's compile this and perfect 20 just as you add just as we expected for now because we borrowed right we should be able to use the value afterwards so what we did here with printing yeah we should be able to do this here right and this is we couldn't do that to do this essentially with a move right because move have to move the whole ownership is gone but now the ownership after my function is ready here right it will be turn it back to this scope so now I can rinse twice 2020 no problem okay so this read boring so with we boring you can also borrow it multiple times and so let's check it as possible so we're going to create a little scope here to borrow value and little bit of sale okay a board it's read borrow this V gonna borrow this see and even afterwards by doing that come to CD you can borrow it is start with that so we can borrow it and D it's indeed right so very simple very clear for right so if we want to execute this then of course you get a lot of warnings because the underscores but it makes sense right so I can do like endless amount of reads but what I cannot do is mutate essentially the previous velm right so even if I would make a mutable of all of this your problems will get a little error here yeah move this booth but so now essentially it still works right but now after I borrowed this in a will be whatever so now when I borrow it the value here right in this code and I would use it afterwards like the original failure I would like change like so now for example I would change here the value so I would say okay let's let's change this 20 inside the box with 40 and then I would let see now use a and you use essentially a borrowed value and which it would be like borrow again right then we have a problem right because now I use the borrow so this is essentially the scope of the ball inside the scope of the borrow I change the original value of being being changed right so this is not a lot right so let's check this and say no borrow uses here and of course at 9:30 which is like 30 is here here recording borrow and now we did an assignment write 2 to the power value and then I use the power later right that's at line 35 which is here right so but if I really would remove this one right then there is no problem right then it's just ok no because then essentially there's no problem because I never use it right so it's the virus really smart about this one so what we can say here is a reborrowing we can say the following right and reborrowing we can say only a 10% yeah and we say that means no rights no move no moves as long as the Baro is that right so now we come to the other kind of boring and that is mutable borrowing right and this will be signified by this symbol and percent mute and what this means is essentially okay you mutiply borrow the value but now we do not allow no exit at all so that means no rights and no reads other than the person that did put the borrowing very important no exit at all no rights no weeds so let's see how this works let's just remove this function now because the mutable borrowing has do a lot with the Scopes so we made a mutable of it so let's now say okay a is immutable let's remove all this stuff and it also means that we need to use a so we think like that B is missing right so now it means that because we brought immutably here a has exclusive access to the value so now here a has exclusive access to the variable so that means no writing all really except through a and the reason for this is because Ross cannot allow to have a function for example that takes and a mutable reference like a mutable ball and an immutable borrow at the same time right so because that would lead to all kinds of race conditions right we want to avoid right so how come in test is right so let's compile this okay nothing here so let's just print let's print something here so this is value fee for example right so what I'm writing here right now is okay I have a mutable bar here which gives exclusive exit to a but I try to access the same memory space essentially by V and this is not allowed by Rosco file so let's see and yes it says a mutable borrow occurs here and now suddenly do you try to immutably with the courtesy right that's that's not allowed so to summarize we have three boring which allows no rights no moves but reads right then we have mutable boring boring which does not allow any access at all and no moves so no exit all no rights no reads and no moves so just a global recap again rust and ownership so first we have the concept of moving we have the three golden rules each resource at a variable called its owner there can be only one owner at a time very important and when an owner goes out of scope the resource is destroyed and a transfer of ownership means that the original owner is completely irrelevant right it's a very simple idea with a very powerful because it avoids a lot of complications and then we have the concept of borrowing where we can classify two kinds we have the rebar ring and the mutable boring and the rebar ring means at the in the moment your boring in between death life time essentially no rights no moves as long as the bar is active and then when you do mutable bar a then essentially you have exclusive rights with only death owner and I say that power to the variable nobody can even read to it right so this is the essential of rusts ownership system
Info
Channel: Boy Maas
Views: 1,122
Rating: undefined out of 5
Keywords: rust, coding, live programming
Id: 7e_6zTbXdhU
Channel Id: undefined
Length: 15min 58sec (958 seconds)
Published: Thu Apr 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.