1 00:00:06,300 --> 00:00:12,120 So let's start off by looking at vectors and look at some of the methods that are associated with the 2 00:00:12,120 --> 00:00:12,780 vector. 3 00:00:13,020 --> 00:00:21,900 So we'll create a mutable vector called nums, and we're going to just put in values of AI 32 and we 4 00:00:21,900 --> 00:00:25,560 want our vector to be empty to start. 5 00:00:27,150 --> 00:00:30,300 So we looked, we saw this earlier in the course. 6 00:00:30,300 --> 00:00:34,500 We can use a push method to add values into the vector. 7 00:00:34,500 --> 00:00:40,200 So let's do that and let's just add in a few values here. 8 00:00:47,320 --> 00:00:55,960 So one way that we can remove values off the top is by using a method called pop, and this actually 9 00:00:55,960 --> 00:00:58,060 returns a value. 10 00:00:58,060 --> 00:01:06,730 So we can actually say let pop equals number pop, and then we can print out this value. 11 00:01:13,290 --> 00:01:14,700 It's not implement. 12 00:01:14,700 --> 00:01:15,980 So there we go. 13 00:01:15,990 --> 00:01:17,430 So now we can run this. 14 00:01:18,280 --> 00:01:20,860 We can see that we returned some three. 15 00:01:20,860 --> 00:01:24,650 So this returns an option of type T. 16 00:01:24,670 --> 00:01:34,660 So remember this can either return none or some T, and in this case it popped some three. 17 00:01:35,560 --> 00:01:38,920 So think of this as a stack of plates. 18 00:01:38,920 --> 00:01:40,180 So we're going to. 19 00:01:41,060 --> 00:01:43,130 Push plates onto the top. 20 00:01:43,130 --> 00:01:46,910 And then when we pop, we want to take off the last plate. 21 00:01:46,910 --> 00:01:50,900 So in this case, three that we pushed on to the stack. 22 00:01:52,950 --> 00:02:02,130 So we can also, just as a review, reference some of the numbers so we can say let two equals num one. 23 00:02:03,060 --> 00:02:07,950 And since we're doing I 32, this would just be a simple copy. 24 00:02:08,880 --> 00:02:19,560 But if we were doing another value that doesn't implement copy, we could create a reference to it in 25 00:02:19,560 --> 00:02:20,460 this case. 26 00:02:20,880 --> 00:02:28,560 So creates a reference if copy is not available. 27 00:02:31,180 --> 00:02:39,610 And then just to show you that we were able to successfully copy it, we'll say too and we'll run it. 28 00:02:39,730 --> 00:02:41,490 And we got two. 29 00:02:41,500 --> 00:02:42,310 So great. 30 00:02:42,670 --> 00:02:43,390 So. 31 00:02:44,150 --> 00:02:48,010 There's a couple of other ways that we're able to return values. 32 00:02:48,020 --> 00:02:53,030 One of them being let one equals numb dot first. 33 00:02:53,030 --> 00:02:59,930 And as you can imagine, this is going to return to us the first value and our vector. 34 00:03:00,530 --> 00:03:09,500 But again, this is going to return an option T, so it's going to be none if the vector is empty 35 00:03:12,350 --> 00:03:15,050 or some t. 36 00:03:17,400 --> 00:03:18,090 If. 37 00:03:19,920 --> 00:03:25,050 Now if the vector is not empty because we're going to take out the first index. 38 00:03:25,770 --> 00:03:30,000 So we want to return the we want to get the first value. 39 00:03:30,090 --> 00:03:38,100 So let's make sure that one successfully did just that for so one. 40 00:03:42,390 --> 00:03:44,670 And we returned some one, which is great. 41 00:03:44,670 --> 00:03:47,400 That's the expected value that we were anticipating. 42 00:03:47,700 --> 00:03:54,630 So we can also do that last and just like first, instead of it being first, this is going to return 43 00:03:54,630 --> 00:03:56,970 the last value in the vector. 44 00:03:57,960 --> 00:04:06,660 But we also have to first mute and last mute. 45 00:04:06,870 --> 00:04:13,590 And as the name suggests, it's going to be the same as first and last, but it's going to borrow mutable 46 00:04:13,590 --> 00:04:14,640 references. 47 00:04:15,180 --> 00:04:20,250 Will borrow mutable references. 48 00:04:22,320 --> 00:04:31,620 So if we want to check out the length of our vector, we can do nums, dot length, line short for length. 49 00:04:31,800 --> 00:04:39,080 And then if we want to do a quick check to just see if our vector is empty, we can do nums dot is empty 50 00:04:39,090 --> 00:04:51,090 and this is going to return a boolean, whereas this will actually return a value of the length. 51 00:04:54,420 --> 00:05:01,320 Another way that we can add values inside a vector is using insert, but insert is going to allow us 52 00:05:01,320 --> 00:05:02,160 to. 53 00:05:03,470 --> 00:05:08,690 Insert at the index of our choice so we can say insert at. 54 00:05:09,900 --> 00:05:10,860 Index zero. 55 00:05:10,860 --> 00:05:23,010 So the first index and we want to insert ten, but we can also say nums dot insert at the three and 56 00:05:23,010 --> 00:05:30,210 we'll put in 12 and then we can do nums dot insert two. 57 00:05:31,790 --> 00:05:35,120 And we can put in the value of 25. 58 00:05:39,750 --> 00:05:46,830 So like insert we can remove with nums dot remove and then we can just specify the index that we want 59 00:05:46,830 --> 00:05:47,190 to remove. 60 00:05:47,190 --> 00:05:50,070 So in this case, let's remove index three. 61 00:05:54,080 --> 00:05:57,620 And then if we want to sort everything out, we can do nums dot sort. 62 00:05:57,620 --> 00:06:06,590 And now let's look to see how it would appear sorted just to make sure everything looks good. 63 00:06:06,650 --> 00:06:09,200 And now we're going to do nums. 64 00:06:11,330 --> 00:06:12,380 So we run it. 65 00:06:12,380 --> 00:06:16,070 We see we have 110, 12 and 25. 66 00:06:17,420 --> 00:06:18,500 Looks sordid to me. 67 00:06:19,130 --> 00:06:26,150 And we can also run a method such as reverse, which is going to do what we expect it to do, which 68 00:06:26,150 --> 00:06:27,220 is just going to reverse it. 69 00:06:27,230 --> 00:06:34,910 So in this case, since we've already sorted it, I expect it to be 25, 12, ten and one, which is 70 00:06:34,910 --> 00:06:35,180 great. 71 00:06:35,180 --> 00:06:36,680 That's exactly what we got. 72 00:06:37,370 --> 00:06:42,950 So something else that we can do is we can actually have our vector shuffled. 73 00:06:42,950 --> 00:06:49,700 So in order for us to do this, we need to go into our cargo file and we need to specify that we want 74 00:06:49,700 --> 00:06:53,240 to bring in the RAND package. 75 00:06:53,630 --> 00:06:59,390 So we're going to say Rand and then we're going to do version 0.8.4, which is the latest version of 76 00:06:59,390 --> 00:07:01,850 Rand at the time of this recording. 77 00:07:02,960 --> 00:07:04,280 We'll save that file. 78 00:07:05,070 --> 00:07:11,400 And then we'll come to the top outside of Maine and we're going to say, use rand. 79 00:07:11,490 --> 00:07:15,420 Seek and slice random. 80 00:07:15,690 --> 00:07:18,030 And then we're going to say use rand. 81 00:07:19,260 --> 00:07:19,960 Thread. 82 00:07:19,980 --> 00:07:20,400 Rand. 83 00:07:22,320 --> 00:07:25,080 And now down here, we can say nums, dot shuffle. 84 00:07:26,850 --> 00:07:37,350 And it's going to say want a mutable reference to the value generated to us by thread random number 85 00:07:37,350 --> 00:07:37,920 generator. 86 00:07:37,920 --> 00:07:40,020 That is what RNG stands for. 87 00:07:42,000 --> 00:07:51,150 And now if we print this out, it's going to shuffle our vector for us and we'll run this a couple of 88 00:07:51,150 --> 00:07:53,520 times to make sure that it is being shuffled. 89 00:07:55,190 --> 00:07:58,220 And we got ten, 25, one and 12. 90 00:07:59,140 --> 00:08:06,130 And then we got 12 one, 25 and ten, and then we got ten, 12, 25 and one. 91 00:08:06,130 --> 00:08:10,000 So it does appear to be shuffling as we intended. 92 00:08:10,910 --> 00:08:15,740 So these are just some examples, examples of what vectors have to offer in rust. 93 00:08:15,770 --> 00:08:22,070 There are many more methods we can use, so I do encourage you to play around with all the methods associated 94 00:08:22,070 --> 00:08:22,990 with vectors. 95 00:08:23,000 --> 00:08:26,750 That way you become familiar with all the. 96 00:08:27,720 --> 00:08:29,460 Methods we have at our disposal.