Learning Rust from Reddit?!


English (Auto-generated)

for everyone, it is his um today we're going to be looking at the learn rest, learn rust subreddit, which is a community on Reddit for asking questions and getting free help with Rust. By the way, there might be a lot of background noise, it's raining here, um I was trying to wait for it to stop raining and looks like it's just not gonna stop, so I kind of give up on that and I guess we're gonna record, so I'll check the audio after this, hopefully uh hopefully I'm audible so and weird bug here, but I don't know if you notice it says I'm live but zero time, so I guess we'll see how that works anyway. So yeah, this is the learn Rust subreddit, people come on here and ask questions. It's um we've been focusing a lot on how to learn resources that are great for learning more about rust. And this is a great one. You see first rust program, we have this one already open how to find the sum of numbers and you can go in and help people, you can go in and get help, you can go in and just kind of lurk, it's great to see other people have problems and you can learn from that. So yeah, let's uh let's take a look at this, let's see what's going on, how to find the sum of numbers in an array. So it sounds like they're gonna add all the numbers up, right? But when we look over here, we see we have 8, 72 and then a target which is 10. The output is eight and 2. So it looks like you have to go through an array and find which pairs within that array can add up to your target. And yeah, some people give that as a tip. But maybe we could actually write the code, you know? Yeah, maybe he wants to see what the actual code looks like. Already got 23 responses so we can't do any harm. Alright, let's take a look. So let's let's try this. We're gonna take something to get what is this? eight seven to Well, just try to find which of these add up which pairs here Out of the 10. Or whatever other parameter they give us because obviously we do it as a function. So, first I'm gonna I'm gonna create a vector. I'll say what the equal, create a little vector. Beautiful. Yeah. And we'll write a function that you write a function that gets all of the pairs. And then we'll figure out which pairs go to our target. So let's make a function called Get hairs. You know what? Let's just go straight into it. Why do we try to skip anything? Let's just make one called? Uh get some. So we'll take a vector about V. I'm gonna put the type as vector. Um Normally we would do a reference to the vector like that. But actually, I mean, I guess we could just move the vector. It's not like, sorry man, this is just test data but you know what we should do good for him. So we'll take take this vector of numbers to your reference that we we can just borrow it. We don't have to take ownership of it. Then we have the actual target which is gonna be an integer. And what do we return? Well, if we look over here it might have you might have multiple pairs that add up. Right? But all that up for the same thing. So what will return is will return a new vector? It will consist of basically that will consist of a bunch of couples or pairs managers. Alright beautiful. The first thing we're gonna do is make our new vector. The thing we're gonna end up returning. So say what I don't know pairs before and we can uh man you guys are gonna kill me but I forget the Sorry. So embarrassing. I always use that little black thing. Yeah that new. There we go embarrassing. And stop push to put it back in. Always forgetting the list impacted things like that in the works. Okay? So Now we will say four. Um Well just say for I in v dot dinner and we'll have to do a loop inside. We can just go through each one. We need to go through each one and compared to each other ones that we can see which things add up to the target. Then we'll say forward hey the dark hater. And then we'll say if I plus J equals our target and that's that's it. That's the magic sauce. If I plus I don't know, I hope I'm not getting a cold from the draining weather most all of a sudden. Sorry. So that means this is legit there so we can push it to our parents. Yeah, I think you think I should specify type here. I'm just gonna note that this is a vector that kicks in. Uh But I guess I could just try it and see what the compiler did, but now we'll do this. I'm pretty sure we should. So if that's the case then we'll get I and now since we're using better, this is gonna give us like a reference so we'll actually have to de reference these. Alright, that should actually do the trick. Right? I mean, let's see, so we can go ahead and say just return our pairs. That was easy. No offense to the guy who made that. Got in trouble with this easy because we're used to coding in rust. So we will say um I guess let pairs equal that some, we will pass it a reference to be actually won't be using the anymore. So we could just move ownership of V and to get some but I like to I created the function that way. Just imagining like what if you know what if this is gonna be integrated, another code. I think that's always how we should be thinking as developers. So I will say for P in our pears turn it into an inter give us a bunch of pupils and we can say um we can do our print line. We'll make it look kind of how it's supposed to look. So it's supposed to output is supposed to be hair found or And then the next pair, I'm just gonna skip the ore was actually join these together with yours. And it just feels silly. I'd like check if I'm on the last one and all of that pair found. And the other thing that will consist of key.0 And p.1. Wonderful cargo run and see what happens. Of course of course it doesn't work. Cannot borrow as mutable. That's because I'm trying to push into this but it's not mutable. So we can add much that it's mutable. Taking a million years is a good sign or a bad time. Right? So let's see. Are these all valid? Um So we have 8273 and 55. So some problems here um we're getting repeats. Obviously that's no good. Um eight and 2 and two and 8 are the same thing basically. So what we could do to handle that is we could just we can only we could loop through like half of it and then only with the other half. I don't know. I don't know. Um We can see if it's already in there. I'm sure Russ has a good way to do that. And five and five. We obviously don't want to be doing it with itself. So that's not good. But we basically worked. I mean we are able to find them, right? That's the thing we initially wanted. So it's not a bad thing, but it is it is an issue. Uh Let's see. So we're starting with a la we eventually get to two. So yeah, I guess what we can do is go through the rest of B. So see how we're looping through V. Right? We start at the beginning and go to the end. We could say, okay, check eight against everything that's remaining. Now, when we do seven, we don't need to start at the beginning. Right? We just need to say from seven on from two on from five on the way we would do that is by not basically enumerating like using python's enumerate function but for rest. Look at rust, enumerate uh operator. I think it's dot mm But I forget. This guy says it's dot enumerate which sounds pretty legit to me. So let's give it a shot. Alright. I'm gonna try dot enumerate and I believe I can use a slice, slice notation on a vector. I've never tried this. I just never had to Yeah, slice tight. It will discuss these collections. I guess it works. I just need to like I don't know. We'll figure it out. But the first thing I need to do is this is not gonna give me the index and I was telling it I but that seems a little confusing. I'll just keep going and I okay. And I don't I don't actually need this one. Well actually I do because I'm going to compare them. So I call this index. I we're gonna compare the indexes that we don't do the same thing. Like we do five and five we're gonna prevent that. But making sure the indexes aren't the same. Okay. Yes. Yes yes yes. Yes. All right. Trust the vector slice notation. I was in an unexpected direction. How do I get a slice of a vector? Okay. It looks like it works basically basically the way you expect. So let's try it. We call this index J Can we get you numerator? Only thing is we don't want just uh we wanna start at index I. Okay. Yeah. In fact I guess if we started the next one then we don't have to worry about them ever being the same because they can go to the end like that. I don't know. I'm kind of pushing my my memory here. I'm somewhat of a rust beginner myself so we'll see how this works. We're learning together. We're learning together everybody. Alright let's see what happens. I'm sure rust is not gonna be happy about this. But that's okay and now we wouldn't need anymore so we can just get j get rid of a lot of ugly code. I love the leading code. Okay. It looks like it's compiling. That's already a good sign. I started with so few so few tabs just always ends up like that. Okay, there you go. It works beautifully. Para found eight and two. Sorry, I'm all surprised. I'm so used to like not can't borrow on move axios. Tokyo, you know, Tokyo is a library for asynchronous trust anyway. Well that's it. Our code works. So this wasn't just about solving a problem though, right? It was about actually teaching someone something. So we'll go ahead and put this on here in line code. We're going to mark, download, say like this, make sure this is Rust code and say, hey there, I'm at I'm also new. This is a fun problem. I mean there's nothing wrong with like why are we able to do this? And he and he or she is not able to do this. It's not because I mean we're all newbies right? It's not like we're some super geniuses or something. It's just because I have experience doing other programming languages and computer science and stuff. So the problem itself was easy for me. As you saw, the parts involving Rust were actually not that easy for me. I have a lot of experience with other languages. So I have head start here is how I solved this. I hope that helps Happy coding. And we leave our comments and there we go. Now. We have a happy person. Excuse me Who has gotten one more responsible from 24, to 24 and we'll give him an up vote because they deserve it. Well, it's been quite the quite the video. Huh? I hope this was educational for you. I hope you had fun. I certainly did. Um, and I'll see you all again soon. Oh, that was yeah. I must have been saving sometimes. I forget, wow, wow. Words are hard. Sometimes I forget to save my changes in the editor. But obviously I was running it with cargo so I must have been saving it. It still has that weird. zero buck. How strange is that anyway, Nice talking to all. Hope to see you again soon. Bye bye.
89 Views 0 Likes 0 Comments

The reddit.com/r/learnrust subreddit is a fantastic source of free advice

Leave a comment (supports markdown format)