String slicing in Rust

Transcript

English (Auto-generated)

Hey everybody, this is Hayes and today we're going to do string slicing. So if you're familiar with javascript, you might have heard of the dot slice method, it allows you to access, you know, a certain segment of an iterable like an array or a couple or what have you. So instead of getting the whole thing, you just wanna get item zero through nine or what have you. Instead of creating some new array using a loop, you can just say, hey, I want from this part to this part and give me that. And if you, if you use python, you can even specify, I think you can do this in javascript but I can't remember that. You want to jump every other element or go back every other element, every element, things like that. Well rust slicing is actually very simple. So let's just look at a simple Hello World app. And yeah, there we go. But we have a simple Hello World app. Let's try to use sling string slicing to just get this last part. Just the word World. Okay, so we will start out with a string, we'll call it Hello World. And that's the only use string new. Sorry, I have to have a co pilot lines, it's always suggesting things anyway. Okay, so we have Hello World. I just want to get the word World we would say we're gonna borrow. So we're gonna use this app symbol to borrow. The reason we're gonna have to do that is because otherwise we will have taken ownership over this string and won't be able to use it and we won't even be able to use this slice either because it's just a part of that other reference. So anyway as you C. R. A. I actually knew exactly what to do. So I'm just gonna type that again and we'll see what yeah we're gonna start a character six which I'm presuming is the W. And go up to 11. I don't want the exclamation point. So we'll see a different clues that but we'll find out. So let's run cargo run and check this. Oh no I believe everything. Just kidding. All right. So it's mad because string new doesn't take any arguments and that's because string new creates a new blank blank string and it's a string from my apologies and beautiful. We get world world so get hub copilot ai still not as good as humans I guess. There we go. At 6 12. Let's try it one more time. Beautiful. The word world comes up alright. That's pretty simple but that is the gist of it. Um If you look in the rust book it'll talk about slices and it uses an example pretty similar to this. Just basically showing how you can do things with strings. Yeah. Um To give you an idea though. World it's not like in python where it creates a new string that's just consists of these characters. No this is literally a reference to that part of the string. Um To demonstrate that. Let's try saying hello world dot clear, which will clear out our string, let's see what happens with the print world. So we're changing the string and complaining that it's not mutable. So I'm gonna make it mutable, and again it is angry because we're passing into this clear method and it's like, oh hey, what's going on? How am I supposed to still use it? Okay, so and actually sorry that well, okay, that's an issue, right? This is crashing, So we say hello world dot clear, but we don't use it again, so that's fine. Clear is gonna take ownership of this, I mean, I guess we could try to like, you know, borrow it or something. Okay, but the problem is that when we change hello world, since this is a reference to part of Hello World, we're actually changing world whenever we change hello. So even though we try to say, hey, let it uh you know, make it mutable, do all this stuff. The problem is that when we change Hello world, we're also changing world, if that makes any sense. So let's see, immutable borrow occurs here, let's see. Mhm. Yes, so we have to pass that is mute, have to pass. That is mute. Maybe the point isn't to make this work, it's just to show you that Yeah, this is definitely gonna be an issue and we can't let people borrow twice as you can see, this isn't gonna work, like python would because if we try to clear this out, we're actually changing world as well. That's what I wanted to show you. Yeah, Okay, let's get rid of all these mutants have been a bunch of crazy stuff now, so I have to undo all night craziness. Okay, there we go. We got world back. So yeah, this is different from python because when we try to change Hello World, we're also going to be affecting world so that we're gonna have to have that issue in mind whenever we want to update things. I just want to show you that because as you can see if we try to take that knife approach where I just get crazy and I'm like, oh yeah, let me let me clear out Hello World. That's going to change world too. Because these are references to the same thing. This is just a reference to a part of the string. Hello World. It's a slice of that string. It's not a new string created from that slice, like we might expect from, you know, python. All right, well, thank you for your time. I hope that was helpful. And I look forward to exploring the next rust concept. Maybe we'll do something a little more complex or more advanced with these uh slices now that you've had a taste of them. Thanks a lot. Bye
8 Views 1 Likes 0 Comments

Used to string slicing in Python, or the `.slice` method in JS? Rust will be easy to pick up, then!

Comment
Leave a comment (supports markdown format)