Check out a free preview of the full The Rust Programming Language course:
The "Memory 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 Memory exercise. Student questions regarding what sum.0 does, if using an array instead of a vector would bypass the process, and why can you reuse numbers instead of changing it to numbers2 and numbers3 are also covered in this segment.

Get Unlimited Access Now

Transcript from the "Memory Solution" Lesson

[00:00:00]
>> So we've got product of numbs here and we're basically just trying to call, same thing as before here, product numbs semicolon, and then same thing with average and we're gonna do both of these cases get an error. So, oops, well, one of the errors is it's called number errors not numbs.

[00:00:26] Right, so we're getting use of move value numbers values here after moved yesterday, so, how can we fix this, well, the easy way the quick and dirty way is we can just say numbers dot clone. Numbers, clone, oops, and now everything's fine, we're totally good, however, this does of course have a performance penalty to it, I guess we can run this just to make sure everything's working.

[00:00:52] So proud of these numbers, some of these are average these numbers, some of these numbers, great. So everything's working but we do have this performance downside, so let's try doing it the other way, so basically the other way being, let's actually have some of these functions return a tuple.

[00:01:08] So we'll jump down to the sum of numbs function beside the sum function, and basically we're gonna say rather than having a return I 64 we're gonna have a return the 64. Yes, but also additionally a veck of I-64, which is to say the original numbers that we passed in.

[00:01:25] You can do these in either order, you can have it returned the I-64 followed by the thing that we're returning back or the other way around, doesn't matter. I'm gonna go and do the same thing for product because I know we're going to want it so that I-64, I'm not going to bother doing it with average because in this particular example, we don't need to.

[00:01:40] But if I wanted to use this strategy everywhere I would also do it for average, okay, now we see we have some type errors because this is now returning a tuple, but I'm pretending like it's not. So now I want to return some of numbs followed by numbers 2, and I'm gonna use numbers 2 over here.

[00:01:57] So now that one's fixed, but I still have a problem with the other one, which is that I do project numbers come in numbers 3, and then I can use numbers 3 in here. Great, now everything works again, so I have not used any cloning but whoops, [LAUGH] Okay, fair enough, so I guess I actually did that in the wrong place.

[00:02:22] Number 3 is a vector bytes 64 averages numbers three problems, I see some of numbers, right, so yeah, so then here I need to do some of numbers, I guess I can just do dot zero. That'll work cool, okay, so now we have, some of numbers, is the same answers as before, except that we, were able to do it without any cloning questions about this exercise.

[00:02:52]
>> What did some.0 do?
>> Yeah, that was basically, returning the taking the value out of the tuples, so dot zero says give me the first thing in the tuple. And because some is now returning a tuple of I-64 and veck of I-64, I need to get just this part out of the out of the veck, sorry, out of the tuple.

[00:03:13] So .0 is just a quick way to do that, I could have also done like let (answer, numbers2) = sum numbers, yeah, that would have also worked then I could say answer over length. So same basic thing, and then it would complain to me saying, hey, numbers 2 is unused, so I figured rather than writing that, I would just say, some numbers .0, both do the same thing.

[00:03:43] This is going all the way back to part two, which I realized was hours ago at this point, but it's yeah, just a different way to write it.
>> With using an array instead of a vector bypass this process?
>> It would, yeah, absolutely, if we were using an array instead of this, we would not have any of these problems we would not get, you can't, yeah, that would totally not be a problem.

[00:04:04] However, that would mean that numbers would then be have a hard coded length where it would only be able to be a fixed length at compile time. Which as we all know from programming with these types of data structures is not the most useful, it's much more useful if they have a flexible length.

[00:04:19]
>> Thanks, [INAUDIBLE] And I were both able to just reuse the name numbers instead of numbers 2, and numbers 3 seems a little weird because we didn't declare let mute numbers, but I don't know if you could explain how that works.
>> Sure, yeah, so you can actually do this if you want, instead of numbers, 2 numbers 3, so basically what's happening when you do this is it's not actually mutating it, but rather it's shadowing it.

[00:04:43] So essentially what this is saying is it's saying, from now on, I am defining a new thing called numbers, this old thing is no longer exists. The old the old thing known as numbers, is dead to me and now for like Long live this new definition of the word numbers, and same thing here.

[00:05:03] So basically we're sort of like kicking this thing out of scope and replacing it with a new thing that has nothing to do with the old one. In contrast, mutating is saying I wanna actually go in and make a change to this value, like whatever this thing refers to.

[00:05:18] So where this distinction potentially makes a big difference is let's say you do it inside of a for loop, so it's basically the difference between saying like, let x= 5x =5 and let y=5. Let y =5 so, if I do well first of all, it's gonna give me a compile error on this one, but not on the second one, if you can imagine now let's suppose I put a for loop on this, like I don't know, numbers.

[00:05:50] And versus if I put a for loop on this one, then you can kind of see the difference more clearly, because in this top one, I'm actually like inside each iteration of the loop. I'm going back and changing the original one which after I exit the loop, sort of effects what this value is from now on, whereas in this one I'm just defining a new thing that's like shadowing the previous one, and when I exit the loop, the original one still has its original value.

[00:06:17] So that's really the distinction here is when you are, if you decide to make a lead or any kind of naming that sort of reuse an existing name. What that does if they're in the same scope is it just says, Well, from now on, you can't access the previous one.

[00:06:32] Whereas this version actually overrides the previous one, which in an example like this, maybe doesn't matter. But inside a for loop or something like that it can't make it or is that a conditional would be another case where that would make a big difference. So, yeah, I don't want to go into huge change on this but hopefully that [LAUGH] clarifies a little bit.