1 00:00:05,930 --> 00:00:11,480 I hope you enjoyed this assignment and the two questions associated with it, and that you were able 2 00:00:11,480 --> 00:00:14,630 to complete both questions without any problem. 3 00:00:15,530 --> 00:00:20,960 In this video, we are going to go over the solution to both questions and the assignment. 4 00:00:21,740 --> 00:00:29,090 The first question is create a function that takes one argument called Val that is of type vector with 5 00:00:29,090 --> 00:00:32,810 the values one, three, five and seven. 6 00:00:35,030 --> 00:00:45,230 So we will create our function and we will call it Czech Val and it is going to be up type vector. 7 00:00:45,530 --> 00:00:49,100 So we know that and it has values one, three, five and seven. 8 00:00:49,100 --> 00:00:51,650 So it is a type I eight. 9 00:00:52,340 --> 00:00:58,820 And then inside of this function, check the first value of the vector and see if it is equal to one. 10 00:00:59,120 --> 00:01:03,740 At the value is equal to one, then return true, otherwise return false. 11 00:01:05,540 --> 00:01:07,970 So we know that we're going to need to return a boolean. 12 00:01:08,960 --> 00:01:15,220 And so now we want to check if Val of zero is equal to one. 13 00:01:15,230 --> 00:01:21,950 If it is return true else return false. 14 00:01:22,640 --> 00:01:28,130 So now up here we are going to create our vic and we can use the VEC macro for this and we are going 15 00:01:28,130 --> 00:01:31,370 to have one, three, five and seven. 16 00:01:33,090 --> 00:01:38,040 So now we want to print out if our value 17 00:01:40,680 --> 00:01:41,730 is true or not. 18 00:01:44,700 --> 00:01:56,070 So we are going to call check Val and we will say on vector and then add the value 15 to the vector 19 00:01:56,070 --> 00:02:09,960 so we can do push 15 and then to confirm our results, we want to print out the vector. 20 00:02:12,540 --> 00:02:16,170 So will this code compile or not? 21 00:02:19,290 --> 00:02:26,820 It does not compile because vector does not implement the copy trait because it is not a simple type. 22 00:02:27,030 --> 00:02:35,640 So in order for us to pass the vector into this function and then still be able to use it after this 23 00:02:35,640 --> 00:02:40,020 function call, we need to pass it in by reference. 24 00:02:41,670 --> 00:02:44,790 That way the function down here called Check. 25 00:02:44,790 --> 00:02:49,810 Val does not take ownership of our vector which we created here. 26 00:02:49,830 --> 00:02:55,410 So now if we go to run this, it will not execute fine. 27 00:02:55,410 --> 00:02:58,350 Because I forgot the mute keyword. 28 00:02:58,410 --> 00:03:04,590 Since we are pushing a value in, we have to make it a mutable variable. 29 00:03:04,920 --> 00:03:09,120 And now if we run it, it will execute perfectly fine. 30 00:03:10,110 --> 00:03:13,210 And this is a good segway in the question too. 31 00:03:13,230 --> 00:03:16,980 So create a function called add two. 32 00:03:20,340 --> 00:03:29,520 And this function is going to take one parameter that is of IE eight and we're going to add two to it. 33 00:03:31,110 --> 00:03:34,080 So we'll say Val plus two. 34 00:03:35,430 --> 00:03:38,670 And in this case, we don't care about returning our value. 35 00:03:38,970 --> 00:03:54,540 So up here we want to say let mute value equal to two and now we want to call add two on value. 36 00:03:54,900 --> 00:04:00,930 And what I want to do here is now I want to say value is equal to five. 37 00:04:01,290 --> 00:04:08,760 So will this compile since we are not passing in value by reference. 38 00:04:09,240 --> 00:04:11,010 So let's see what we get. 39 00:04:12,480 --> 00:04:26,490 So we have a need to not return a value so we get no errors when we try to re access value here. 40 00:04:26,490 --> 00:04:32,430 And the reason to that and this is going to answer the question of do you have to pass the value by 41 00:04:32,430 --> 00:04:37,470 reference in order for you to maintain ownership of it inside of main? 42 00:04:37,680 --> 00:04:44,460 And the answer to that question is no, you do not have to pass the value by reference. 43 00:04:45,450 --> 00:04:53,460 There is a caveat for that because you do not have to pass simple types that implement the copy trait. 44 00:04:53,460 --> 00:04:57,990 And we will talk more about the copy trait later on in this course. 45 00:04:57,990 --> 00:05:07,200 But for now, I want to open up this concept to you that certain values are considered simple, so integers 46 00:05:07,200 --> 00:05:08,160 are simple. 47 00:05:09,810 --> 00:05:14,070 I ate I 32, i 64 and so on. 48 00:05:14,070 --> 00:05:19,470 And it's simple because it is easy to copy those values. 49 00:05:20,100 --> 00:05:28,800 So when you pass in this case an I eight into a function, you do not and I repeat, you do not have 50 00:05:28,980 --> 00:05:34,500 to pass it by reference in this case because it implements the copy trait. 51 00:05:34,500 --> 00:05:39,630 So that is just something that I wanted to introduce to you now, and it's going to be something that 52 00:05:39,630 --> 00:05:42,420 we cover on later on in this course.