Borrowing in Rust

Transcript

English (Auto-generated)

it says today we're gonna learn about borrowing. Alright. So if you've done anything in Rust or even just read the rest book, you've definitely heard of borrowing. It's basically a feature of ownership where you have this concept that one variable owns a piece of data. And memory is freed when the when its owner goes out of scope. Okay. So what do you mean by that? Well, we have this Hello world code here. Let's create some data and give it a variable that will own it. So we'll say I don't know, you know, make a name. It will be a string that should not it's a name owns john that means here when the name goes out of scope, uh this memory is going to be freed. All right. No problem. But let's say we decided to do something like make a function that I don't know called say name. And this function takes a string. Sorry, it's a string of name. That's why I make sure I have on my syntax right on how I'm taking that argument. Okay, so it takes a string and we do something like Yeah. Hello name. Right, Okay. No big deal. So let's try it. Let's say say it all looks good here. I think we're good to go cargo run. It's Rust. So I'm sure I did something horribly wrong. It's about to crash epically. Okay good. We have Hello, name. Right. No problem. And then we decide. Okay. Well that's what this function says the name was but maybe maybe that function had some complicated code and we don't really trust it. So we decided to print it out here too. Mhm. You see the name, is that alright? So we say name in here, we pass it name and then we're gonna use name up here and yeah, value borrowed here after move what the heck is that? Okay. Remember how I said a variable has has one owner. Right? Mhm. Just to make these not have the same name because this is a local scope. It's actually a different variable in this. Okay. So so when this goes out of scope it gets freed. Right? But we passed this same string, this is a reference to this memory. You can say right? Like this is a string in memory. So we passed that string reference to that string and then we say hey well now this is pointing to that string. So if this goes out of scope, the string is freed and that means we can't use it over here because it's been freed. It went out of scope. So in other words we make the we make this resource here, we create this string. We pass it here. We passed ownership over to the same name function and say name says okay I'll do something with it. And hey went out of scope and free that string and then we go and try to use it out here and it says hey yeah you moved it so that this end is its owner now not name in other words only one owner poor per resource in this case our owner was named and we said no, now we're gonna move it. So the owner is n and now we try to use name again. It's like, well, no, you gave it a new owner. So that's ownership. But this video is not called ownership in rust. It's called borrowing and rust. So in other words, what's the solution? Well, if they're, what if there was a way we could temporarily give ownership and say, hey, at the end of this, instead of freeing the resource past ownership back out here? Well, there is a way to do that. And the way is with borrowing and basically what we do is instead of passing name, we pass a reference to name. Like passing a pointer if you've ever used C or C plus plus. And instead of bringing the researchers will just free the pointer and now we're going to have a pointer to a string. Okay. And let's see how that goes for us. Look at that. Hello, john the name is john beautiful. In other words, instead of the reason this works is this is a reference, right? This is like a pointer to, to some other resource and now it's gonna free that reference, but not the thing itself and this is called borrowing. That's it, we lend it over and it will say, okay, I'll give the original thing back when I'm done and then it does that and we can now still use it. Um, if you're still stuck on this, I highly recommend the references borrowing and borrowing section of the Rust programming language. The book, also known as the Rust Book. It's a great resource. You should check it out anyway. Thank you a lot for your time. I hope this was as educational for you as it was for me when I learned about it. Thanks a lot. Bye.
44 Views 1 Likes 0 Comments

One of the most confusing aspects of Rust for newcomers - *demystified* in today's Rust codecast!

Comment
Leave a comment (supports markdown format)