1 00:00:06,630 --> 00:00:09,660 - In this lesson, we're going to look at array slices. 2 00:00:09,660 --> 00:00:12,240 An array slice is similar to a string slice 3 00:00:12,240 --> 00:00:14,430 except that you're peeking into an array 4 00:00:14,430 --> 00:00:15,930 instead of into text. 5 00:00:15,930 --> 00:00:18,450 Okay, so first of all, let's have a quick recap 6 00:00:18,450 --> 00:00:20,970 of what we know about arrays already. 7 00:00:20,970 --> 00:00:24,150 An array is a fixed-size homogeneous collection. 8 00:00:24,150 --> 00:00:25,950 Each element is the same type. 9 00:00:25,950 --> 00:00:28,170 You can create an array using literal syntax, 10 00:00:28,170 --> 00:00:31,260 square brackets containing the values. 11 00:00:31,260 --> 00:00:32,220 Here's an example. 12 00:00:32,220 --> 00:00:35,793 A is an array, contains five values, integers. 13 00:00:37,440 --> 00:00:42,090 The memory for an array is allocated on the stack 14 00:00:42,090 --> 00:00:44,640 and deallocated at the end of the function. 15 00:00:44,640 --> 00:00:46,650 Okay, so it's locally allocated, 16 00:00:46,650 --> 00:00:48,690 doesn't get allocated on the heap, 17 00:00:48,690 --> 00:00:50,970 it's allocated on the stack. 18 00:00:50,970 --> 00:00:52,320 Good to know. 19 00:00:52,320 --> 00:00:54,360 You can create an array slice. 20 00:00:54,360 --> 00:00:57,093 You prefix the array name with an ampersand. 21 00:00:57,930 --> 00:00:59,370 It looks like this. 22 00:00:59,370 --> 00:01:04,030 In this example, my array is here 23 00:01:06,660 --> 00:01:11,660 and my slice s kind of points into the array 24 00:01:14,850 --> 00:01:18,300 and it also kind of knows how big the slice is as well. 25 00:01:18,300 --> 00:01:21,480 We'll talk about that in a bit more detail later on, 26 00:01:21,480 --> 00:01:25,320 but it's like a viewport into the actual data in the array, 27 00:01:25,320 --> 00:01:28,080 either the whole array or part of the array. 28 00:01:28,080 --> 00:01:29,670 If you use this syntax, 29 00:01:29,670 --> 00:01:32,640 then it's a slice of the whole array. 30 00:01:32,640 --> 00:01:35,163 S is basically the whole array content. 31 00:01:36,390 --> 00:01:38,070 So s is an array slice. 32 00:01:38,070 --> 00:01:40,950 It borrows the whole of the array here. 33 00:01:40,950 --> 00:01:43,710 I haven't specified like particular indexes 34 00:01:43,710 --> 00:01:46,353 so it effectively receives the whole of the array. 35 00:01:47,580 --> 00:01:52,020 If you want to, you can explicitly type an array slice 36 00:01:52,020 --> 00:01:54,450 using this syntax ampersand 37 00:01:54,450 --> 00:01:55,890 and then inside square brackets 38 00:01:55,890 --> 00:01:58,740 the type of the elements in the array. 39 00:01:58,740 --> 00:02:00,870 Okay, so here's an example. 40 00:02:00,870 --> 00:02:03,873 Consistent and actually equivalent to the previous slide. 41 00:02:04,860 --> 00:02:06,720 A is my array. 42 00:02:06,720 --> 00:02:10,810 And so a kind of looks like this 43 00:02:12,360 --> 00:02:15,350 and s borrows the array, 44 00:02:15,350 --> 00:02:18,060 or if you like is a slice of the array. 45 00:02:18,060 --> 00:02:19,950 So if you want to explicitly type it, 46 00:02:19,950 --> 00:02:24,950 you'd say it is borrowing an array of 32 bit integers. 47 00:02:25,050 --> 00:02:28,770 Okay, so s is kind of like a slice 48 00:02:28,770 --> 00:02:32,070 or a borrow of the data inside the array. 49 00:02:32,070 --> 00:02:34,860 And by default, it sees the whole of the array 50 00:02:34,860 --> 00:02:36,720 if you write it like that. 51 00:02:36,720 --> 00:02:41,403 Okay, so what information does an array slice contain? 52 00:02:42,810 --> 00:02:45,930 It contains two pieces of information. 53 00:02:45,930 --> 00:02:48,570 The address of an element in the array, 54 00:02:48,570 --> 00:02:50,010 may be the first element, 55 00:02:50,010 --> 00:02:51,600 may be a different element, 56 00:02:51,600 --> 00:02:55,380 and the length of the slice in elements. 57 00:02:55,380 --> 00:02:59,463 So if your array looks like this, 58 00:03:00,390 --> 00:03:02,640 you could have a slice that says 59 00:03:02,640 --> 00:03:05,280 I'm going to point to the first element 60 00:03:05,280 --> 00:03:10,050 and the slice will contain all the elements like so. 61 00:03:10,050 --> 00:03:12,510 Alternatively, you could have a slice 62 00:03:12,510 --> 00:03:15,210 that points to that element 63 00:03:15,210 --> 00:03:18,336 and only has two elements in the slice. 64 00:03:18,336 --> 00:03:21,420 Okay, so you can specify the extent of the array 65 00:03:21,420 --> 00:03:23,883 that you want to view via the slice. 66 00:03:24,960 --> 00:03:26,700 So here we go. 67 00:03:26,700 --> 00:03:30,030 You can access the pointer and the length. 68 00:03:30,030 --> 00:03:32,070 This is similar to what we had with a string slice. 69 00:03:32,070 --> 00:03:35,850 In this example here, I've got an array, five elements. 70 00:03:35,850 --> 00:03:39,690 I have a slice that basically views the whole array. 71 00:03:39,690 --> 00:03:42,450 When I get the address, when I take my slice, 72 00:03:42,450 --> 00:03:44,430 it has an as pointer function. 73 00:03:44,430 --> 00:03:47,490 It'll give me the address of the first element. 74 00:03:47,490 --> 00:03:49,050 I'm gonna say length. 75 00:03:49,050 --> 00:03:51,780 It'll give me the length of the array in elements, 76 00:03:51,780 --> 00:03:53,910 not bites, but elements. 77 00:03:53,910 --> 00:03:54,930 Okay, so it'll give you 78 00:03:54,930 --> 00:03:57,603 the length of the slice here would be five. 79 00:03:58,710 --> 00:04:00,510 Right, well, let's have a look at an example. 80 00:04:00,510 --> 00:04:03,180 We'll have a look at a simple example first, 81 00:04:03,180 --> 00:04:04,330 demo_array_slice_intro. 82 00:04:05,490 --> 00:04:07,863 We'll run the project when we've seen the code. 83 00:04:08,700 --> 00:04:12,570 So here we are then, here's the project in main. 84 00:04:12,570 --> 00:04:16,317 I'm going to uncomment this function call 85 00:04:16,317 --> 00:04:18,990 demo_array_slice_intro. 86 00:04:18,990 --> 00:04:22,030 And it's here 87 00:04:23,610 --> 00:04:25,710 and I'm going to run it. 88 00:04:25,710 --> 00:04:27,360 It's quite straightforward this example actually, 89 00:04:27,360 --> 00:04:28,860 so I'll run it and then we'll just discuss 90 00:04:28,860 --> 00:04:29,960 the output at the end. 91 00:04:31,380 --> 00:04:32,313 cargo run. 92 00:04:35,730 --> 00:04:38,433 Okay, so here's my array. 93 00:04:39,600 --> 00:04:41,430 I can take a slice of the array, 94 00:04:41,430 --> 00:04:45,630 s1 is a slice of the whole array implicitly typed. 95 00:04:45,630 --> 00:04:49,110 And I display the address of the first element, 96 00:04:49,110 --> 00:04:52,380 the length of the slice in elements, 97 00:04:52,380 --> 00:04:56,760 and then the data in the slice, like so. 98 00:04:56,760 --> 00:04:59,430 So it'll output the as pointer, 99 00:04:59,430 --> 00:05:04,110 will give back the address of the first element. 100 00:05:04,110 --> 00:05:08,970 The length, it gives me the length of my slice in elements. 101 00:05:08,970 --> 00:05:12,720 And you can output a slice using the debug formatter. 102 00:05:12,720 --> 00:05:14,760 Remember that, the debug formatter? 103 00:05:14,760 --> 00:05:17,100 You can use that for a slice as well. 104 00:05:17,100 --> 00:05:21,600 So it'll output the elements that you see in your slice, 105 00:05:21,600 --> 00:05:26,160 so s1 was implicitly typed as a reference to an array, 106 00:05:26,160 --> 00:05:28,800 an array slice, in other words. 107 00:05:28,800 --> 00:05:32,460 s2 is explicitly typed as a reference 108 00:05:32,460 --> 00:05:35,853 into a 32 integer array. 109 00:05:37,140 --> 00:05:41,400 So s2, same idea, it's just using explicit type in here. 110 00:05:41,400 --> 00:05:44,790 When I output s2, it's gonna give me the same information. 111 00:05:44,790 --> 00:05:47,280 It's just using explicit type in 112 00:05:47,280 --> 00:05:48,780 rather than implicit type in. 113 00:05:48,780 --> 00:05:52,320 My choice would be to use inferred type in here, 114 00:05:52,320 --> 00:05:54,090 easier on the syntax. 115 00:05:54,090 --> 00:05:56,400 You can write it that way if you want. 116 00:05:56,400 --> 00:05:59,250 Being able to explicitly specify 117 00:05:59,250 --> 00:06:03,420 the type of a slice is very useful if you had a function. 118 00:06:03,420 --> 00:06:07,350 You could have a function that took a slice as a parameter. 119 00:06:07,350 --> 00:06:08,640 So that would be the syntax, 120 00:06:08,640 --> 00:06:11,250 that would be the time when you'd use the explicit syntax 121 00:06:11,250 --> 00:06:13,320 to specify type in as an array, 122 00:06:13,320 --> 00:06:17,130 as a parameter that you pass into a function. 123 00:06:17,130 --> 00:06:20,220 Okay, so that's a simple example of slices. 124 00:06:20,220 --> 00:06:21,720 What we'll do in the next section 125 00:06:21,720 --> 00:06:25,500 is have a look at how to use some more additional techniques 126 00:06:25,500 --> 00:06:27,153 on top of array slices.