I hope you enjoyed this assignment and the two questions associated with it, and that you were able
to complete both questions without any problem.
In this video, we are going to go over the solution to both questions and the assignment.
The first question is create a function that takes one argument called Val that is of type vector with
the values one, three, five and seven.
So we will create our function and we will call it Czech Val and it is going to be up type vector.
So we know that and it has values one, three, five and seven.
So it is a type I eight.
And then inside of this function, check the first value of the vector and see if it is equal to one.
At the value is equal to one, then return true, otherwise return false.
So we know that we're going to need to return a boolean.
And so now we want to check if Val of zero is equal to one.
If it is return true else return false.
So now up here we are going to create our vic and we can use the VEC macro for this and we are going
to have one, three, five and seven.
So now we want to print out if our value
is true or not.
So we are going to call check Val and we will say on vector and then add the value 15 to the vector
so we can do push 15 and then to confirm our results, we want to print out the vector.
So will this code compile or not?
It does not compile because vector does not implement the copy trait because it is not a simple type.
So in order for us to pass the vector into this function and then still be able to use it after this
function call, we need to pass it in by reference.
That way the function down here called Check.
Val does not take ownership of our vector which we created here.
So now if we go to run this, it will not execute fine.
Because I forgot the mute keyword.
Since we are pushing a value in, we have to make it a mutable variable.
And now if we run it, it will execute perfectly fine.
And this is a good segway in the question too.
So create a function called add two.
And this function is going to take one parameter that is of IE eight and we're going to add two to it.
So we'll say Val plus two.
And in this case, we don't care about returning our value.
So up here we want to say let mute value equal to two and now we want to call add two on value.
And what I want to do here is now I want to say value is equal to five.
So will this compile since we are not passing in value by reference.
So let's see what we get.
So we have a need to not return a value so we get no errors when we try to re access value here.
And the reason to that and this is going to answer the question of do you have to pass the value by
reference in order for you to maintain ownership of it inside of main?
And the answer to that question is no, you do not have to pass the value by reference.
There is a caveat for that because you do not have to pass simple types that implement the copy trait.
And we will talk more about the copy trait later on in this course.
But for now, I want to open up this concept to you that certain values are considered simple, so integers
are simple.
I ate I 32, i 64 and so on.
And it's simple because it is easy to copy those values.
So when you pass in this case an I eight into a function, you do not and I repeat, you do not have
to pass it by reference in this case because it implements the copy trait.
So that is just something that I wanted to introduce to you now, and it's going to be something that
we cover on later on in this course.