1 00:00:05,740 --> 00:00:09,940 Another way to have a collection of multiple values is with an array. 2 00:00:10,920 --> 00:00:12,300 Like a tuple. 3 00:00:12,930 --> 00:00:15,840 Arrays are going to have a fixed length. 4 00:00:16,990 --> 00:00:25,180 But an array is different from a tuple because every element of an array must have the same type. 5 00:00:26,040 --> 00:00:30,160 Now there are a couple of different ways that we can set up an array. 6 00:00:30,180 --> 00:00:37,680 With the first one being, let's create an array variable, give it some brackets, and then we're going 7 00:00:37,680 --> 00:00:40,440 to create an array that holds integers. 8 00:00:40,950 --> 00:00:43,590 So here we created our first array. 9 00:00:44,910 --> 00:00:48,390 And assigned values one, two and three inside of it. 10 00:00:49,020 --> 00:00:56,280 Now, like a tuple arrays are also going to start counting from zero when you are referencing their 11 00:00:56,280 --> 00:00:57,090 index. 12 00:00:57,210 --> 00:01:05,640 So index zero is going to contain the value one index one is going to contain the value two and so on 13 00:01:05,640 --> 00:01:06,570 and so forth. 14 00:01:07,500 --> 00:01:18,840 So if we wanted to access one of the values inside the array, we will say array, open and close bracket, 15 00:01:19,650 --> 00:01:21,900 and then we will give the index that we want. 16 00:01:22,530 --> 00:01:29,910 So what this is going to do is it's going to access the first element in the array force, which in 17 00:01:29,910 --> 00:01:33,150 this case is one, and that's exactly what we got. 18 00:01:34,260 --> 00:01:41,610 Another way that we can set up an array is saying, Hey, I want an array, but this time I want it 19 00:01:41,610 --> 00:01:50,550 to be assigned integer 32 bits and then I want it to have a specified length of three. 20 00:01:51,090 --> 00:01:53,460 And then in here you do the same thing. 21 00:01:53,460 --> 00:01:54,660 So we'll give it another. 22 00:01:55,920 --> 00:02:04,710 Three values, and in this case it is four, five and six, and it works the same exact way as before. 23 00:02:05,640 --> 00:02:14,730 So now what happens if we want to modify a value inside of an array? 24 00:02:15,600 --> 00:02:21,690 So if we came down here and said array two of index zero equals. 25 00:02:22,750 --> 00:02:23,440 Ten. 26 00:02:24,220 --> 00:02:29,140 That would be me trying to change the value four to the value ten. 27 00:02:29,530 --> 00:02:33,610 So do you think this will work or will we get an air? 28 00:02:36,340 --> 00:02:39,130 If you said you think we will get an error? 29 00:02:39,250 --> 00:02:40,810 You are correct. 30 00:02:41,570 --> 00:02:46,160 Because we did not make our array mutable. 31 00:02:46,750 --> 00:02:52,270 And luckily, if you happen to make this mistake, the rust compiler is friendly enough to tell you 32 00:02:52,270 --> 00:02:55,450 and it tells you, hey, you need to make your array mutable. 33 00:02:55,570 --> 00:03:00,070 So now if we go up here, make it mutable, and then try it again. 34 00:03:03,240 --> 00:03:04,130 We're good to go. 35 00:03:04,140 --> 00:03:09,420 So we changed the first value in the ray from 4 to 10. 36 00:03:10,330 --> 00:03:17,170 Now, another thing is, is what happens if we try to reference an index outside of the array? 37 00:03:17,200 --> 00:03:24,640 Meaning, since we only have three values, the highest index we should technically be able to go is 38 00:03:24,640 --> 00:03:25,300 to. 39 00:03:25,970 --> 00:03:33,140 But what happens if we tried to reference index number three, which does not exist in this case? 40 00:03:33,260 --> 00:03:36,050 So if we do that, what do you know? 41 00:03:36,350 --> 00:03:40,070 The rust compiler is smart enough to tell us. 42 00:03:40,070 --> 00:03:40,580 Hey. 43 00:03:41,870 --> 00:03:43,730 Prior to runtime you. 44 00:03:44,440 --> 00:03:47,140 Try to reference an index that. 45 00:03:47,820 --> 00:03:48,930 Doesn't exist. 46 00:03:50,200 --> 00:03:51,640 So it's already doing that. 47 00:03:51,640 --> 00:03:52,750 Checking for us. 48 00:03:52,750 --> 00:03:54,070 But what happens? 49 00:03:54,920 --> 00:03:58,130 If we did it dynamically at runtime? 50 00:03:58,130 --> 00:04:02,300 Well, it tells us right here this operation will panic at runtime. 51 00:04:02,300 --> 00:04:08,690 And that is really good for us because we can actually handle panics in our code, which we will get 52 00:04:08,690 --> 00:04:10,490 to later in this course. 53 00:04:11,450 --> 00:04:14,840 And the big the big deal behind this is is it's going to. 54 00:04:15,960 --> 00:04:21,360 Prevent the user and our program from being able to access. 55 00:04:22,320 --> 00:04:24,120 Unauthorized memory. 56 00:04:25,020 --> 00:04:33,360 So the rest compiler is really good and it's also very smart at being able to check our code prior to 57 00:04:33,360 --> 00:04:38,760 runtime to hopefully help us catch bugs before we put them into production.