1 00:00:06,600 --> 00:00:07,800 - So far in the course, 2 00:00:07,800 --> 00:00:10,260 we've been looking at simple types such as integers 3 00:00:10,260 --> 00:00:13,410 and floats and Boolean and characters. 4 00:00:13,410 --> 00:00:15,990 But Rust provides many different ways 5 00:00:15,990 --> 00:00:18,900 to group data together into aggregate variables 6 00:00:18,900 --> 00:00:21,480 which makes life much more interesting. 7 00:00:21,480 --> 00:00:25,410 So for example, you can create an array or tuple. 8 00:00:25,410 --> 00:00:27,810 An array is a fixed-size construct 9 00:00:27,810 --> 00:00:29,970 where each element is the same type. 10 00:00:29,970 --> 00:00:32,910 And a tuple is a fixed-size construct 11 00:00:32,910 --> 00:00:34,980 where you can have different types of data. 12 00:00:34,980 --> 00:00:38,520 So a tuple, for example, element zero might be a string, 13 00:00:38,520 --> 00:00:40,380 element one might be an integer, 14 00:00:40,380 --> 00:00:42,510 element two might be a float. 15 00:00:42,510 --> 00:00:45,090 Okay, you can modify them potentially 16 00:00:45,090 --> 00:00:47,340 if you declare them as mutable. 17 00:00:47,340 --> 00:00:48,870 You can't change their size. 18 00:00:48,870 --> 00:00:50,910 You can just change the values. 19 00:00:50,910 --> 00:00:53,130 So as well as arrays and tuples, 20 00:00:53,130 --> 00:00:55,770 we also have various different collection classes 21 00:00:55,770 --> 00:00:57,960 such as vector and map. 22 00:00:57,960 --> 00:01:02,340 A vector is a resizable collection, like a resizable array, 23 00:01:02,340 --> 00:01:06,300 and a map is a dictionary with keys and values. 24 00:01:06,300 --> 00:01:08,490 So first of all, arrays. 25 00:01:08,490 --> 00:01:11,190 An array is a fixed-size homogeneous collection 26 00:01:11,190 --> 00:01:14,420 where each element is the same type and it's fixed-size 27 00:01:14,420 --> 00:01:16,890 so you can't extend it afterwards. 28 00:01:16,890 --> 00:01:18,870 You can use square brackets syntax 29 00:01:18,870 --> 00:01:21,483 to create a literal array, as I've said, shown here. 30 00:01:23,190 --> 00:01:25,380 If you want the array to be mutable, 31 00:01:25,380 --> 00:01:27,510 then just use the mut keyword 32 00:01:27,510 --> 00:01:29,490 where you declare your array variable 33 00:01:29,490 --> 00:01:32,040 that allows you to change the values inside the array. 34 00:01:32,040 --> 00:01:34,500 Although, like I said, you can't change its size. 35 00:01:34,500 --> 00:01:37,170 If you want resize ability, then you should use vector 36 00:01:37,170 --> 00:01:39,240 and would have a look at that later. 37 00:01:39,240 --> 00:01:40,500 So here's an example. 38 00:01:40,500 --> 00:01:42,390 I've declared a variable a1. 39 00:01:42,390 --> 00:01:45,487 It's an array of integers, an array of i32. 40 00:01:46,950 --> 00:01:51,330 a2 is mutable, so I can change the values in a2. 41 00:01:51,330 --> 00:01:53,613 I can't change the values in a1. 42 00:01:54,630 --> 00:01:57,780 Right, to use an array, square brackets. 43 00:01:57,780 --> 00:01:59,400 You specify the index. 44 00:01:59,400 --> 00:02:01,350 The index must be within range. 45 00:02:01,350 --> 00:02:04,650 So if you have an array of length 10, for example 46 00:02:04,650 --> 00:02:07,860 first element is zero, last element is nine, 47 00:02:07,860 --> 00:02:10,380 and Rust will basically give you an error 48 00:02:10,380 --> 00:02:12,120 if you exceed the bounds. 49 00:02:12,120 --> 00:02:16,800 If you use a literal index like index 11 50 00:02:16,800 --> 00:02:18,690 and the compiler knows for a fact 51 00:02:18,690 --> 00:02:20,340 that you've exceeded the limits, 52 00:02:20,340 --> 00:02:22,920 then you'll get a compile time error. 53 00:02:22,920 --> 00:02:27,270 If you have a variable index i at runtime, 54 00:02:27,270 --> 00:02:29,670 bound checking will still take place. 55 00:02:29,670 --> 00:02:31,950 Okay, and your application will terminate 56 00:02:31,950 --> 00:02:34,650 if you exceed the bounds, so don't. 57 00:02:34,650 --> 00:02:37,320 If you're not sure, then you get the length of the array 58 00:02:37,320 --> 00:02:38,580 using the length function. 59 00:02:38,580 --> 00:02:42,750 So technically, the array construct has a length method, 60 00:02:42,750 --> 00:02:44,400 gives you the length of the array 61 00:02:44,400 --> 00:02:47,670 and then you can use that in your error checking. 62 00:02:47,670 --> 00:02:49,830 Right, most commonly with an array, 63 00:02:49,830 --> 00:02:51,450 you will iterate over the elements. 64 00:02:51,450 --> 00:02:53,520 You can use a for end loop like this. 65 00:02:53,520 --> 00:02:55,043 So if you have an array called arr, 66 00:02:56,160 --> 00:02:59,490 each element would be copied into the elem variable here 67 00:02:59,490 --> 00:03:00,930 and then you can process the elements, 68 00:03:00,930 --> 00:03:03,210 print them and modify them potentially 69 00:03:03,210 --> 00:03:04,620 if the array is mutable. 70 00:03:04,620 --> 00:03:06,690 So it's all kind of quite straightforward really, 71 00:03:06,690 --> 00:03:09,510 pretty much what you might expect from other languages. 72 00:03:09,510 --> 00:03:11,280 So we'll have a look at an example. 73 00:03:11,280 --> 00:03:13,140 The project for this lesson 74 00:03:13,140 --> 00:03:14,840 is lesson05_compounds_collections. 75 00:03:17,010 --> 00:03:19,020 So it talks about compound data types 76 00:03:19,020 --> 00:03:21,180 like arrays and tuples, 77 00:03:21,180 --> 00:03:24,540 and it covers collections like vector and map. 78 00:03:24,540 --> 00:03:26,130 And in that project, 79 00:03:26,130 --> 00:03:27,480 I've got a bunch of different functions. 80 00:03:27,480 --> 00:03:29,280 For this particular section, 81 00:03:29,280 --> 00:03:31,770 the function we're gonna look at is called demo_arrays. 82 00:03:31,770 --> 00:03:33,210 So we'll go through the code 83 00:03:33,210 --> 00:03:35,820 and then we'll run the project as normal. 84 00:03:35,820 --> 00:03:37,173 So let's have a look. 85 00:03:38,040 --> 00:03:40,680 This is how it should look on your file system. 86 00:03:40,680 --> 00:03:43,860 The Rust dev demos folder, 87 00:03:43,860 --> 00:03:48,240 lesson five, as I said, this is a cargo project, 88 00:03:48,240 --> 00:03:49,403 lesson05_compounds_corrections, 89 00:03:50,460 --> 00:03:53,430 and let's open that up in the code editor. 90 00:03:53,430 --> 00:03:58,430 So here we are, simple main file, main.rs, 91 00:03:58,800 --> 00:04:03,060 and in here, I've imported the HashMap structure. 92 00:04:03,060 --> 00:04:05,100 We'll have a look at that towards the end of this lesson. 93 00:04:05,100 --> 00:04:07,050 We'll ignore that for now. 94 00:04:07,050 --> 00:04:08,820 I've got various different functions 95 00:04:08,820 --> 00:04:10,680 that we're gonna call from main, 96 00:04:10,680 --> 00:04:12,660 same kind of arrangement as usual. 97 00:04:12,660 --> 00:04:15,450 First function we're gonna call demo_arrays 98 00:04:15,450 --> 00:04:16,413 and here it is. 99 00:04:18,540 --> 00:04:20,370 So I'm gonna run the application first, 100 00:04:20,370 --> 00:04:21,810 usual kind of arrangement 101 00:04:21,810 --> 00:04:23,510 so that we kind of see the output. 102 00:04:25,170 --> 00:04:27,783 Cargo run. 103 00:04:30,210 --> 00:04:35,130 And at the bottom here in the arrays function Using Arrays, 104 00:04:35,130 --> 00:04:36,780 here we are. 105 00:04:36,780 --> 00:04:40,983 So I've declared a immutable array, a1, 106 00:04:41,940 --> 00:04:43,980 containing those three values. 107 00:04:43,980 --> 00:04:46,067 You can get the length of the array, 108 00:04:46,067 --> 00:04:48,780 the length of the array is obviously three. 109 00:04:48,780 --> 00:04:51,690 It starts indexing at zero, like most languages. 110 00:04:51,690 --> 00:04:54,330 So the first element is element zero, 111 00:04:54,330 --> 00:04:56,490 that's 100, that's correct. 112 00:04:56,490 --> 00:04:57,900 All good. 113 00:04:57,900 --> 00:04:59,640 Here's a mutable array. 114 00:04:59,640 --> 00:05:01,920 So remember, you can change the values, 115 00:05:01,920 --> 00:05:04,020 but you can't change the size. 116 00:05:04,020 --> 00:05:07,860 So I'm gonna change element zero to be 999. 117 00:05:07,860 --> 00:05:10,770 Let's get the length and the first element. 118 00:05:10,770 --> 00:05:13,680 The length of a2 is three 119 00:05:13,680 --> 00:05:15,453 and the first element is now 999. 120 00:05:15,453 --> 00:05:17,250 That is correct. 121 00:05:17,250 --> 00:05:20,130 And then we can iterate over the array, 122 00:05:20,130 --> 00:05:24,600 so give me each element in a2, so that's my a2, 123 00:05:24,600 --> 00:05:27,300 and print each element on the console. 124 00:05:27,300 --> 00:05:29,790 So those are the three elements displayed. 125 00:05:29,790 --> 00:05:32,070 Okay, so that's a quick introduction to arrays. 126 00:05:32,070 --> 00:05:34,560 What we're gonna do in the next section is go deeper 127 00:05:34,560 --> 00:05:36,260 and look at some array techniques.