1 00:00:05,820 --> 00:00:09,810 Vectors are a resizable array of elements allocated on the heap. 2 00:00:10,140 --> 00:00:15,030 Vectors are an essential type and rust because they are used almost anywhere. 3 00:00:15,030 --> 00:00:17,160 One needs a list of dynamic size. 4 00:00:17,160 --> 00:00:23,070 The easiest way is to use the VEC macro and as a reminder, we will talk more about macros later on 5 00:00:23,070 --> 00:00:23,820 in this course. 6 00:00:23,820 --> 00:00:28,590 But for now let's just look at how to create a vector using the VEC macro. 7 00:00:29,100 --> 00:00:35,970 So I went ahead and created a new variable called nums and we are going to use the VEC macro to create 8 00:00:36,600 --> 00:00:37,200 a vector. 9 00:00:37,200 --> 00:00:40,440 So I'm going to put in the values one, two and three. 10 00:00:40,590 --> 00:00:49,980 And now remember I said that vectors are resizable, so let's add a value into this vector already and 11 00:00:49,980 --> 00:00:52,980 call it and give it the value four. 12 00:00:54,000 --> 00:00:58,830 So now let's print this out and see how it works. 13 00:01:04,500 --> 00:01:11,130 Now this is going to produce an error, but I want you to see the error so that we can understand why 14 00:01:11,130 --> 00:01:11,880 it's an error. 15 00:01:12,150 --> 00:01:17,550 So it says vec of type integer cannot be formatted with the default formatter. 16 00:01:17,700 --> 00:01:24,390 So if we continue reading down and this is just showing you how helpful the rust compiler is, it will 17 00:01:24,390 --> 00:01:28,770 say you may be able to use a colon and a question mark. 18 00:01:29,740 --> 00:01:30,640 Instead. 19 00:01:30,670 --> 00:01:36,820 So basically what that is, is that's going to allow us to put it into debug mode. 20 00:01:37,450 --> 00:01:39,190 That's what the question mark is. 21 00:01:39,190 --> 00:01:47,830 And the the colon basically says, take the next thing and then push it into the formatter and output 22 00:01:47,830 --> 00:01:49,390 it into debug mode. 23 00:01:49,720 --> 00:02:00,130 So now that we have that in there and we cargo build, don't forget to make your variable mutable since 24 00:02:00,130 --> 00:02:04,030 we are changing it by adding it another number. 25 00:02:09,040 --> 00:02:13,570 And now we can see we have the values one, two, three and four printed out. 26 00:02:15,130 --> 00:02:22,090 But we can also remove values, so we can use the pop method that a vector provides us. 27 00:02:22,990 --> 00:02:29,950 And what pop is going to do is it's going to take the last value that we pushed into the vector and 28 00:02:29,950 --> 00:02:30,640 remove it. 29 00:02:31,300 --> 00:02:34,690 So let's pop one and then let's print it out. 30 00:02:34,900 --> 00:02:42,430 And it should remove the four from the vector, which is exactly what it did, which is awesome. 31 00:02:43,990 --> 00:02:47,180 So there is another way that we can create vectors. 32 00:02:47,200 --> 00:02:49,060 So let's go ahead and do that. 33 00:02:49,060 --> 00:02:53,710 And it's called Vec Nu. 34 00:02:55,380 --> 00:03:00,420 So when we use the macro, this is essentially. 35 00:03:01,400 --> 00:03:05,420 What it's calling is the new constructor. 36 00:03:05,540 --> 00:03:12,380 So now that we created a new empty vector, again, we can push values onto here. 37 00:03:13,220 --> 00:03:18,470 So let's call it test and then let's push on. 38 00:03:21,020 --> 00:03:21,860 String. 39 00:03:24,850 --> 00:03:26,350 Let's print it out. 40 00:03:27,100 --> 00:03:31,240 Just to make sure that we are working correctly. 41 00:03:34,710 --> 00:03:35,670 Clear it. 42 00:03:36,240 --> 00:03:36,960 Build it. 43 00:03:37,530 --> 00:03:38,100 Run it. 44 00:03:38,100 --> 00:03:39,570 And we have test string. 45 00:03:39,690 --> 00:03:45,600 So now what's really cool about vectors, they come with a ton of methods that we can already use. 46 00:03:45,600 --> 00:03:51,810 So I highly encourage you to explore all these methods after this lecture, just to really familiarize 47 00:03:51,810 --> 00:03:55,260 yourself with everything available in the vector class. 48 00:03:55,440 --> 00:04:00,210 So what we're going to do, just for an example, is reverse. 49 00:04:01,770 --> 00:04:03,060 Vector that we created. 50 00:04:03,570 --> 00:04:07,680 So by doing this, we should see when we print it out. 51 00:04:07,680 --> 00:04:09,000 String test. 52 00:04:11,770 --> 00:04:13,060 Which is exactly what we got. 53 00:04:13,060 --> 00:04:14,500 We have string test. 54 00:04:15,340 --> 00:04:21,010 So again, I highly encourage you that after this lecture just play around with all the different methods 55 00:04:21,010 --> 00:04:28,360 that a vector provides and just really get familiar with them as they will help you in your rest career. 56 00:04:29,200 --> 00:04:34,300 So another really cool thing that we can do is if you know the size that your vector is going to be, 57 00:04:34,300 --> 00:04:36,280 we can actually specify that. 58 00:04:36,490 --> 00:04:45,430 So let's start off with a new vector and we'll call this one vector and we're going to say Vec. 59 00:04:45,850 --> 00:04:51,820 Now we have to give it a type since we are creating it empty in this case. 60 00:04:51,820 --> 00:05:02,320 So I'm going to give it integer 32 with capacity two and then I would just want to print out just to 61 00:05:02,320 --> 00:05:04,090 make sure that it created. 62 00:05:05,760 --> 00:05:10,080 That capacity effect dot. 63 00:05:13,220 --> 00:05:14,540 Book passage. 64 00:05:19,670 --> 00:05:20,390 Here we go. 65 00:05:22,970 --> 00:05:24,320 So let's build it. 66 00:05:24,800 --> 00:05:26,270 We have an issue here. 67 00:05:26,540 --> 00:05:30,230 It says, use fully qualified syntax. 68 00:05:31,140 --> 00:05:33,130 So we said, let me vet. 69 00:05:33,150 --> 00:05:35,790 Vet of type I. 70 00:05:35,820 --> 00:05:37,110 32. 71 00:05:38,310 --> 00:05:40,350 With capacity to. 72 00:05:47,390 --> 00:05:51,290 Let's see if we can use the compiler to figure this issue out. 73 00:05:51,290 --> 00:05:53,480 So I know that this one's not going to work. 74 00:05:54,420 --> 00:05:56,790 And it's because we needed. 75 00:05:58,140 --> 00:05:59,580 Greater than and less than around it. 76 00:05:59,580 --> 00:06:04,880 So now that we use the compiler to figure it out, let's go ahead and run it. 77 00:06:04,890 --> 00:06:10,530 So down here it says to which is what we set our capacity to be at. 78 00:06:11,490 --> 00:06:18,330 So you might be wondering, well, if I create a vector with capacity to what happens if I end up needing 79 00:06:18,570 --> 00:06:19,980 extra capacity? 80 00:06:20,370 --> 00:06:26,280 Luckily the vector can grow if it needs to, and the way it works is it's going to allocate a larger 81 00:06:26,280 --> 00:06:33,960 buffer, copy the present contents into the new buffer, and then it's going to update our vector variable 82 00:06:33,960 --> 00:06:39,060 to point to the new buffer, and then it's going to free the old vector from memory. 83 00:06:39,060 --> 00:06:44,520 So basically it's just going to get a new region of memory, push everything into it, and then delete 84 00:06:44,520 --> 00:06:45,150 the old. 85 00:06:45,750 --> 00:06:50,640 So that way it's very seamless for us to be able to expand it as we need to. 86 00:06:50,640 --> 00:06:55,470 And then one other trick I want to show you is you can actually create a vector. 87 00:06:57,300 --> 00:07:03,480 Using an iterator and we will talk about iterations later on in this course. 88 00:07:04,020 --> 00:07:13,050 So we'll say we want to create a vector from value 0 to 5, not including five. 89 00:07:14,860 --> 00:07:18,430 And let's go ahead and print it out. 90 00:07:19,090 --> 00:07:31,450 And it should give us values zero, one, two, three and four, which is exactly what we got down here. 91 00:07:31,450 --> 00:07:33,940 We have values zero, one, two, three and four. 92 00:07:34,360 --> 00:07:43,000 So again, I highly encourage you to play around with all the methods available to vectors to really 93 00:07:43,000 --> 00:07:44,290 familiarize yourself with them. 94 00:07:44,290 --> 00:07:51,700 It is a very essential type in rust and it will immensely help you as you continue to learn rust. 95 00:07:52,180 --> 00:07:55,720 And the next lecture, we will talk about what slices are.