Transcript from the "Primitives Exercise" Lesson
>> All right, so for the exercise I've got VS code open here. This is a fresh VS code install that only has a theme and rust analyzer installed. So everything that you're gonna see me do when talk through these things is just using the rusty analyzer plugin. So, Russ analyzer is like the rust like language or one of the rust language server things.
[00:00:23] There's also RMLS. But rust analyzers a lot faster, at least by reputation. Okay, so I have here I've opened part one in. So when you download the repository, you'll see a directory called part one. As you can imagine, there's also part two through seven, and those would be the exercises for those.
[00:00:40] Each of these exercises has the same structure, so there's a source main.rs. If you wanna know how to build it, there's a read me that just kind of explains it. But basically, all you've got to do at the command line is run cargo run. If you're in VS code, there's actually a rust analyzer, I can actually press run right here.
[00:00:57] So basically all of these exercises have the same form. Which is we've got some rust code, and there's some to do's in there. Well, these little emojis so you can see what's going on there. So here we have three different to do's. So basically we have our main function so this is what's going to kick off the program.
[00:01:13] All it does is it prints out says hey, the city of and then city name is Rossville. And then we're calling this print population function. Passing some number of adults, some number of kids and some number of buildings. So adults is a use 64 kids is a u 32.
[00:01:27] Buildings as a u 32. Rust analyzer one of the things that it does is like if I hover over these types anywhere in here. It'll tell me the types of adults use 64 because it knows that because of what we've defined up here, but that also works the type inference.
[00:01:39] You can also see, for example, that although I didn't put a type here for a population, it actually inferred when you can see in gray there it says colon i32 in gray. That's just roughly analyzer saying, I know what type was inferred for that thing. So I'm just going to tell you, if you want, I can also say insert explicit type and then it'll just bop just put it in there and make it be a the actual type.
[00:01:58] So this is kind of a way that you can sort of get the type inference benefit but also still be able to see the types if you want. You can also I think there's a way to turn that off if you don't wanna see so many type annotations.
[00:02:10] Okay, so here's our first to do we wanna compute the population by adding adults to kids. Now because adults is use u 64 and kids uses u 32. We're going to need to use the as keyword to get them both in the same type. We're also gonna wanna compute buildings per person by dividing buildings by population.
[00:02:27] So another tip here about how to use as for that, and finally we're gonna do a little bit string interpolation to print buildings for person right here. So when I run this, we'll see that it's gonna build my code. This takes a little bit, but when I do subsequent builds, it should be a little bit faster.
[00:02:44] And here we see the output of this program, the city of Rustville, population,0, which is wrong, adults, kids, buildings and then we have the two that it's printing out. Once we're done, the population should not be zero. And rather than this, it should print out buildings per person there.
[00:03:00] Okay and again, if you're not using like a VS code, like with this plugin, that's totally fine. All you have to do is run cargo, run at the command line in this directory and this part one directory. And it'll do exactly the same thing.