The Rust Programming Language

Primitives Solution

Richard Feldman

Richard Feldman

Vendr, Inc.
The Rust Programming Language

Check out a free preview of the full The Rust Programming Language course

The "Primitives Solution" Lesson is part of the full, The Rust Programming Language course featured in this preview video. Here's what you'd learn in this lesson:

Richard live codes the solution to the Primitives exercise. Student questions regarding if there are rules for function order in a single file and how cargo know that it's source is main.rs are also covered in this segment.

Preview
Close

Transcript from the "Primitives Solution" Lesson

[00:00:00]
>> So, here we have this population, which we're going to compute by adding adults to kids. Now notice, by the way that we mentioned previously that this was inferring a type of I 32 for this and that's because right now it's just set to be zero. And I 32 is the default integer type that Russ chooses if it doesn't have any other information.

[00:00:16]
Now watch what happens when we change this to be adults. So now, population is now inferred to be u64 because adults is the type u64. And if I try to add it to kids, well, now we're gonna have a problem. So I'm actually going to have a type mismatch because basically cannot add you 32 to u64.

[00:00:34]
Okay, so we need to use as to convert one or the other. Now, as I mentioned previously, if we want we could fix this by converting adults to be a u32. But if adults happens to be bigger than that, then we might lose some information there. So, it'd be better if we converted kids to u64.

[00:00:49]
So, now we've done that populations infer to be u64. And everything's good so far. Okay. So that's what you do down. Next, let's take a look at computing buildings per person. Now here again, we want to end up with an F 64. Because we wanna make sure that we have any like decimal component to this that gets kept around.

[00:01:12]
The reason that this is currently computed inferred to be an F 64. Is this conditional down here we say if buildings per person is greater than or equal to 1.0. And again the default floating point type. So, 1.0 was a floating point literal, and the default floating point time is type is F 64.

[00:01:28]
Okay, so. Let's start by saying buildings over population. And notice that now it changes the inferred type u64. And once again, we have a problem here where these are a mismatch between population which is u64 and buildings u32. But in this case, we want to just convert both of them to f64 using AZ and now that we've done that everything is happy once again.

[00:01:50]
Great. So now, if I were to rerun this again, we would see that this is partially better, but not totally better. Because we now have an accurate population, which is great, but we still have this to do here print billings per person here. And so to fix that, we can just do a little bit of string interpolation in the same example, using the same sort of technique that we've used above.

[00:02:11]
And say buildings per person. And there we go. So now when I rerun, we should see absolutely everything printed out population, adults, kids buildings and buildings per person. Any questions about that solution?
>> I see that we're declaring implementing the print population after mean are there rules about function order in a single file?

[00:02:34]
>> Great question. Are there rules for function order within a single file? Nope, if you want to have these in any order you want like top level functions you can reorder them however you want.
>> How does cargo know, it has to compile source main RS, does it target the files specifically for compilation because it's only of the only RS bottle available.

[00:02:53]
>> Great question. How does cargo know that it's a source main.rs? Main.rs is actually a special name, so it's gonna look for something named specifically names main.rs as the entry point of the program.

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