Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course

The "Count Trees Rust Solution" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through the Rust solution to the count trees problem.

Preview
Close

Transcript from the "Count Trees Rust Solution" Lesson

[00:00:00]
>> And let's create a p3.rs. And now all we have to do is simply grab our input and we can do the exact thing that we just didn't go, but let's use the more iterator kind of fun way to do it. And it will yield a slightly different answer.

[00:00:13]
So I'm gonna jump back to go for a quick second. I'm gonna yap that and then paste in the wrong thing and then paste in the right thing. It's how I assert dominance really. Do that, make sure I have that and of course, return string.arrow. Again, it's an ampersand static string, right?

[00:00:32]
Cuz this reference lives forever because this is a string that's compiled into our program. Going here make that into a double quote this is where I wish I had that surrounds that you're talking about. And there we go, we have this beautiful working function always create a main function because that's what it wants.

[00:00:47]
And so let's just solve it with this right here. So we're gonna go get input. Okay, so remember, everything always think iterators. What is the thing we always call on this block of text as the first thing, was the first thing we do. We got to convert it from a string into an iterator.

[00:01:05]
Anyone know what the first method is? Lines, we got to get each line, right? I was like come on I do this I knew this, right? Okay, so now we're into iterator zone which is it which I know is obviously much nicer. So here's where we're gonna get something kind of interesting.

[00:01:22]
So iterators do not give you your index, unlike the one in JavaScript where I just simply need to add the second parameter, which is actually your index into the array. And even the third one is the array itself. I think there's some fun problem with parse int. And using it over a map because it actually passes in a bunch of things.

[00:01:39]
You'll get a bunch of crazy answers. But anyways, let's go here. So there's another thing called a numerate, which will take an iterator and then attach an index to it. And so now I can have the same thing that I had with for each where I have the I or the for range and go Lang I can now just have it right here.

[00:01:56]
Again, it's not creating copies of an entire array. It's just adding one value, turning it into a tuple and adding an index to it. So it's quite nice. So there we go. So the numerate very fantastic this thing. Man, again, having a great library to manipulate strings or work with iterators makes using a language really simple.

[00:02:16]
I think that this actually really helps. So now that we have the string line plus its index, we can just do that simple, multiply by three yada. So let's go like this. Let's filter. Let's use a filter this time cuz filters feel more exciting. And what is coming into here so we can destructure.

[00:02:35]
Remember rust allows destructuring inside the argument. So we have a, I believe it should be a reference to a tuple of index. And of course line. I wanna say I'm pretty correct on this one. Let's find out. It looks like I am correct on this one. Okay, awesome.

[00:02:56]
So now that we have this, we need to do something with it. So let's go like this. Let's go line. I'm gonna return because, remember, I do not use implicit returns, chars. So that goes over every single UTF-8 character. This is actually gonna properly treat the string so you won't have these weird byte problems going on.

[00:03:13]
And then we need the nth item, right? I want the nth item in this array or in this iterator. It's kind of a cool trick, right? So now I can say, okay, the nth one, of course is gonna be the index multiplied by three module load by line.length, just like we did before.

[00:03:34]
But what does nth return? So let's take a look at it. Let's just we can just jump to its definition. It is an option because it may not exist. So now we kind of have a we have a small problem it's not a big problem but we definitely have a smaller problem here.

[00:03:50]
So what can we do here? Well we can return. We could technically do like is sum. So, I mean, that would work. We could turn this into a nice if statement, right? So I could go let char at equals this and then convert it into snake case because that'd be rude.

[00:04:08]
And then go if let sum char at equals char at and go if. This is a lot stuff right here, right? If char-at equals that, well, then what happens, right? We should be able to do this, but then let's see we should be able to go return true or return false.

[00:04:33]
Yeah, I guess you could do that and saying, hey, this is incorrect because you forgot that. And then I'm saying this little next thing this character is actually not even a string. What are you doing? Okay, you're right. So let's make sure we use a character not a string.

[00:04:45]
And there we go. This actually technically works. We will be getting that character, but honestly, this doesn't necessarily feel that great. Can we all agree this does not feel that great? Okay, good. I'm happy you guys feel the same way. So let's do a flat map instead. Remember, flat maps.

[00:05:01]
If you're seeing maps, what can you do to it? You can squash it in and just make it work. It feels so great. And remember if the thing is none, well guess what? That's okay. It will just ignore it, right? So if we're off the beaten path, it's perfectly fine.

[00:05:14]
So let's return this thing right here. And let's get rid of this for now. So now we have this beautiful thing that is screwed up because I was using a filter. There we go. So now we have this so now the next time we call map, it will have the value from the option because it mapped it in.

[00:05:35]
Which we can also do a filter now. If we do a filter now, what do you think the thing is inside this closure? What type? Can anyone take a wild guess?
>> It's areference to the string or something
>> Man, good job. It's a reference to the character right?

[00:05:58]
That we've just pulled out it's a reference to the nth character cuz remember filters are references. So if I just put an X right here, go this. It's a reference to the character. Great job. That was a really good. You weren't writing any code. So you're like killing it.

[00:06:10]
Does that right? Yeah, all right. So awesome. So we actually have that. So this is good. So I can do something like this. Filter where x equals the octothorpe, right? And there we go so we still have one problem. Of course, we have this thing so we'll just pattern match away the ampersand sign.

[00:06:30]
It's like hey, this thing is unused still, right? We haven't made it, we haven't changed it from an iterator into something that can actually use the iterator, right? So let's go like this. Count, tell me that's not pretty great, right? We just simply need to count the items.

[00:06:48]
We don't need any of the memory, right? We don't need to know. We just need to know that hey, were these ant characters where they pound signs how many nth characters for pound signs. And so there's like a really kind of clever way to get around this. And so let's do a little print LN and do one of these.

[00:07:04]
Go all the way down here and do that. And for fun, let's cargo run this bad boy. P3 we hit a seven. So that's exactly what we were looking for. My goodness, it worked first try, feels good. But I think this probably leads to at least one or two questions because this is very functional, this is very different probably If you're used to the TypeScript world because this certainly does not just exist in regular TypeScript.

[00:07:31]
It's just a count doesn't this whole notion of an iterator needing to convert into a concrete value. I do see some of my functional friends there in chat. I believe they can correct me if I'm wrong. This is called a cat amorphic operation, converting from this kind of iterator type into a single value.

[00:07:48]
I believe I might be correct on that, but I'm not I don't wear a tie dye T-shirts and I'm not ready for the functional world yet.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now