1 00:00:00,180 --> 00:00:06,360 In this lecture, we are going to explore another type for storing a collection of data called vectors. 2 00:00:06,689 --> 00:00:09,750 Typically, vectors are more popular than arrays. 3 00:00:10,020 --> 00:00:12,030 They both serve the same purpose. 4 00:00:12,360 --> 00:00:14,760 A vector can store a list of data. 5 00:00:15,150 --> 00:00:18,240 The data type in a vector must be of the same type. 6 00:00:18,540 --> 00:00:19,870 So what's the deal? 7 00:00:19,920 --> 00:00:21,960 Why is there an alternative type? 8 00:00:22,590 --> 00:00:27,060 Arrays have a fixed size if the size of an array is five. 9 00:00:27,120 --> 00:00:29,820 We can't store a four or six values. 10 00:00:30,150 --> 00:00:37,200 The size must exactly be five, whereas vectors are dynamically sized that are much more flexible than 11 00:00:37,200 --> 00:00:37,800 arrays. 12 00:00:38,130 --> 00:00:41,370 Pushing or removing data from a vector is allowed. 13 00:00:41,670 --> 00:00:44,640 Vectors can be as large as we need them to be. 14 00:00:45,300 --> 00:00:49,110 There are other differences, but this is the most important difference. 15 00:00:49,350 --> 00:00:52,530 It seems like vectors are superior in every way. 16 00:00:52,860 --> 00:00:55,560 However, vectors take up more storage. 17 00:00:55,830 --> 00:01:02,280 Russ needs to allocate memory for a vector, which is not as efficient as arrays, while vectors are 18 00:01:02,280 --> 00:01:04,500 more popular with Russ developers. 19 00:01:04,680 --> 00:01:06,840 There are use cases for arrays. 20 00:01:07,380 --> 00:01:11,220 For example, let's say you're creating a financial application. 21 00:01:11,520 --> 00:01:15,450 Users may want to view the price of a stock over the course of a week. 22 00:01:15,810 --> 00:01:18,360 The stock market is open five days a week. 23 00:01:18,750 --> 00:01:22,080 This type of data can be stored in a vector or array. 24 00:01:22,470 --> 00:01:25,860 However, arrays may be better suited for this task. 25 00:01:26,130 --> 00:01:28,530 We only need to store five values. 26 00:01:28,710 --> 00:01:31,110 Therefore, an array will suffice. 27 00:01:31,740 --> 00:01:35,520 Arrays are great for smaller collections with a fixed size. 28 00:01:35,970 --> 00:01:38,430 Vectors can be used for everything else. 29 00:01:39,630 --> 00:01:40,500 Enough theory. 30 00:01:40,620 --> 00:01:42,270 Let's create some vectors. 31 00:01:42,540 --> 00:01:45,840 Russ gives us two options for creating a vector. 32 00:01:46,140 --> 00:01:49,440 I don't consider either option superior over the other. 33 00:01:49,800 --> 00:01:56,490 Feel free to use whichever syntax you prefer below the items array, let's recreate the same array. 34 00:01:56,880 --> 00:02:00,090 The name of the variable will be called vector items. 35 00:02:02,590 --> 00:02:06,010 The first option for creating a vector is with a macro. 36 00:02:06,250 --> 00:02:09,310 We can set the variable to the vector macro. 37 00:02:11,850 --> 00:02:17,580 This macro doesn't need to be called like a function we can immediately start creating the collection 38 00:02:17,580 --> 00:02:23,910 with a pair of square brackets like before vectors are a list of comma separated values. 39 00:02:24,150 --> 00:02:27,030 Let's input the same values of our array. 40 00:02:27,360 --> 00:02:29,250 Feel free to copy them over. 41 00:02:31,620 --> 00:02:35,580 Next, let's explore the second option for creating a factor. 42 00:02:35,790 --> 00:02:42,840 We're going to recreate the same vector below the variable defining variable called vector items to. 43 00:02:45,240 --> 00:02:51,090 The value for this variable will be a function called the AEC colon colon new. 44 00:02:53,650 --> 00:03:00,220 We're utilizing a feature called Struts, which is short for structures, a structure similar to the 45 00:03:00,220 --> 00:03:02,320 idea of an object or class. 46 00:03:02,650 --> 00:03:05,410 They are containers for multiple pieces of data. 47 00:03:05,680 --> 00:03:08,980 We aren't going to dive into structures for this course. 48 00:03:09,310 --> 00:03:12,550 It's enough to know how to access a function from a structure. 49 00:03:12,820 --> 00:03:15,790 We can access a function by typing to colons. 50 00:03:16,270 --> 00:03:20,800 And this example, we are calling the new function to create a new vector. 51 00:03:21,070 --> 00:03:23,440 By default, the vector will be empty. 52 00:03:23,740 --> 00:03:25,720 We should start pushing values. 53 00:03:25,990 --> 00:03:29,680 Before we do, let's add the immutable keyword to the variable. 54 00:03:29,920 --> 00:03:33,850 Otherwise, we won't be able to push values into the vector. 55 00:03:36,390 --> 00:03:41,730 New values can be inserted into the vector by calling the push function on the vector. 56 00:03:42,060 --> 00:03:44,970 We can pass any value to push into the vector. 57 00:03:45,210 --> 00:03:49,080 Let's push the same five numbers we had in the previous vector. 58 00:03:54,700 --> 00:03:58,000 Creating vectors is a relatively simple process. 59 00:03:58,270 --> 00:04:01,390 There isn't a difference between these two sin taxes. 60 00:04:01,660 --> 00:04:08,260 Behind the scenes, the vector macro is calling the new function to create a vector afterward. 61 00:04:08,410 --> 00:04:11,680 It's calling the push function to add new values. 62 00:04:11,950 --> 00:04:15,520 You can think of it as sugar syntax for creating a vector. 63 00:04:16,120 --> 00:04:19,420 At the end of the day, there isn't a difference in performance. 64 00:04:19,690 --> 00:04:22,210 Choose whichever option feels right to you. 65 00:04:22,480 --> 00:04:27,340 To test our code, we should print these vectors at the bottom of the function. 66 00:04:27,520 --> 00:04:30,070 I'm going to quickly print both vectors. 67 00:04:35,280 --> 00:04:40,920 Since we're dealing with factors, we will need to format the string using the debug placeholder. 68 00:04:41,250 --> 00:04:47,670 Make sure you're typing the colon and question mark characters inside the curly brackets inside the 69 00:04:47,670 --> 00:04:48,480 command line. 70 00:04:48,570 --> 00:04:50,430 Let's compile the project. 71 00:04:53,060 --> 00:04:55,460 Three collections are printed to the console. 72 00:04:55,730 --> 00:05:00,230 The first collection is an array, while the other two collections are vectors. 73 00:05:00,470 --> 00:05:01,760 They look very similar. 74 00:05:02,120 --> 00:05:04,170 Internally, they are very different. 75 00:05:04,460 --> 00:05:07,820 As we discussed before, they have their use cases. 76 00:05:08,420 --> 00:05:10,830 If you need a fixed sized collection. 77 00:05:10,970 --> 00:05:11,860 Use an array. 78 00:05:12,200 --> 00:05:14,240 Otherwise stick with a vector. 79 00:05:14,510 --> 00:05:18,070 Let's continue our discussion of rust in the next lecture.