1 00:00:06,570 --> 00:00:09,420 - If you've created a slice into an array, 2 00:00:09,420 --> 00:00:12,480 you can iterate over the elements in the slice. 3 00:00:12,480 --> 00:00:14,220 No great surprise there. 4 00:00:14,220 --> 00:00:18,720 The slice knows the address of the element, 5 00:00:18,720 --> 00:00:20,640 the first element that it refers to, 6 00:00:20,640 --> 00:00:22,860 it knows the number of elements in the slice, 7 00:00:22,860 --> 00:00:26,040 it might be the whole array or a portion of the array. 8 00:00:26,040 --> 00:00:30,300 and you can iterate over that slice of the data. 9 00:00:30,300 --> 00:00:34,463 So in this example here, my array contains five elements. 10 00:00:34,463 --> 00:00:39,463 10, 11, 12, 13, 14, S is a reference to the array. 11 00:00:40,560 --> 00:00:43,710 In other words, it's a slice to the whole array. 12 00:00:43,710 --> 00:00:47,790 I can iterate over the elements in my slice. 13 00:00:47,790 --> 00:00:49,200 In this case it would be equivalent 14 00:00:49,200 --> 00:00:51,180 to iterating over the whole array. 15 00:00:51,180 --> 00:00:53,760 My slice represents the whole array. 16 00:00:53,760 --> 00:00:56,250 I'm iterating over the elements in the slice. 17 00:00:56,250 --> 00:00:59,340 Remember what's happening under the covers 18 00:00:59,340 --> 00:01:02,460 is that the array would look like this, 19 00:01:02,460 --> 00:01:07,460 10, 11, 12, 13, 14, 20 00:01:07,800 --> 00:01:11,670 and the slice, S, will have a pointer 21 00:01:11,670 --> 00:01:14,580 that points to the first element 22 00:01:14,580 --> 00:01:18,420 and the length here is going to be the whole array. 23 00:01:18,420 --> 00:01:20,610 So when you iterate over a slice, 24 00:01:20,610 --> 00:01:22,350 basically what you're doing is you are iterating 25 00:01:22,350 --> 00:01:26,010 over this portion of the array, 26 00:01:26,010 --> 00:01:26,963 which in this case is the whole array, 27 00:01:26,963 --> 00:01:31,170 10, 11, 12, 13, and 14. 28 00:01:31,170 --> 00:01:33,330 Okay? So obviously the point being 29 00:01:33,330 --> 00:01:36,150 that you could choose a sub array if you wanted to. 30 00:01:36,150 --> 00:01:37,740 Anyway, let's have a quick look at this example 31 00:01:37,740 --> 00:01:39,513 before we get any any further. 32 00:01:40,350 --> 00:01:43,940 So back into the demo project, lesson seven: borrowing. 33 00:01:43,940 --> 00:01:46,830 In my main RS, I'm going to uncomment. 34 00:01:46,830 --> 00:01:49,290 This is the last demo for this lesson. 35 00:01:49,290 --> 00:01:51,661 Demo array slice techniques. 36 00:01:51,661 --> 00:01:54,480 And it's here. 37 00:01:54,480 --> 00:01:57,480 And I've got three functions looking at how to 38 00:01:57,480 --> 00:02:02,010 iterate over a slice, how to get part of an array, 39 00:02:02,010 --> 00:02:05,520 and how to mutate an array via a slice. 40 00:02:05,520 --> 00:02:07,590 So those three functions we're gonna look at 41 00:02:07,590 --> 00:02:09,112 in the next few minutes. 42 00:02:09,112 --> 00:02:12,000 We'll first of all, look at slice iteration 43 00:02:12,000 --> 00:02:13,620 and we'll just ignore these other two functions, 44 00:02:13,620 --> 00:02:14,880 just for now. 45 00:02:14,880 --> 00:02:16,730 Let's have a look at slice iteration. 46 00:02:17,670 --> 00:02:19,500 Quite straightforward. Same as the slide, isn't it? 47 00:02:19,500 --> 00:02:21,180 We've got my array. 48 00:02:21,180 --> 00:02:23,070 I've got a slice over the whole array, 49 00:02:23,070 --> 00:02:25,530 and the elements in the slice, 50 00:02:25,530 --> 00:02:27,780 I can iterate over the elements in the slice 51 00:02:27,780 --> 00:02:30,660 and it will be as if I'm iterating over the whole array. 52 00:02:30,660 --> 00:02:32,580 It'll output all the elements, 53 00:02:32,580 --> 00:02:35,553 10, 11, 12, 13, 14 when I do a cargo run. 54 00:02:38,130 --> 00:02:40,830 So those are the elements in the slice. 55 00:02:40,830 --> 00:02:43,830 10, 11, 12, 13, 14. 56 00:02:43,830 --> 00:02:46,350 Fantastic. So that was easy enough. 57 00:02:46,350 --> 00:02:48,150 Let's just collapse that one down. 58 00:02:48,150 --> 00:02:50,160 What we'll do next is we'll have a look at 59 00:02:50,160 --> 00:02:52,530 how to slice over part of an array. 60 00:02:52,530 --> 00:02:55,080 So let's discuss the theory first. 61 00:02:55,080 --> 00:02:57,592 So you can create a slice as a portion of an array 62 00:02:57,592 --> 00:02:59,520 exactly the same as the the way it works 63 00:02:59,520 --> 00:03:00,870 with a string slice, 64 00:03:00,870 --> 00:03:02,340 except that with an array, 65 00:03:02,340 --> 00:03:04,710 you're always specifying the element numbers. 66 00:03:04,710 --> 00:03:07,091 With a string, it was the bite position, 67 00:03:07,091 --> 00:03:09,930 but with an array, the slice elements, 68 00:03:09,930 --> 00:03:12,660 the indexes are the actual elements themselves. 69 00:03:12,660 --> 00:03:15,570 So when you create a slice, you can specify the start 70 00:03:15,570 --> 00:03:18,740 and the end index as element positions. 71 00:03:18,740 --> 00:03:23,220 The start index is inclusive, starts at zero by default, 72 00:03:23,220 --> 00:03:25,800 the end index is exclusive. 73 00:03:25,800 --> 00:03:27,690 And if you don't specify the end index 74 00:03:27,690 --> 00:03:30,330 then it defaults to the end of the whole array, 75 00:03:30,330 --> 00:03:31,163 what you'd expect. 76 00:03:31,163 --> 00:03:34,800 We do the same idea as you had with string slices earlier. 77 00:03:34,800 --> 00:03:38,460 So in this example, here's my array, A, 78 00:03:38,460 --> 00:03:40,320 and you could specify, 79 00:03:40,320 --> 00:03:43,350 basically you're borrowing a slice of the array 80 00:03:43,350 --> 00:03:46,800 starting at whatever start index you want, inclusive, 81 00:03:46,800 --> 00:03:50,250 dot dot and then the exclusive end index. 82 00:03:50,250 --> 00:03:52,830 So let's see an example of this. 83 00:03:52,830 --> 00:03:53,910 Okay, so here we are. 84 00:03:53,910 --> 00:03:58,910 I'm going to look at the slice part of array and it's here. 85 00:03:59,310 --> 00:04:01,170 So I'll let you look at the code 86 00:04:01,170 --> 00:04:02,527 while I'm doing a cargo run. 87 00:04:02,527 --> 00:04:04,610 (typing) 88 00:04:07,110 --> 00:04:10,980 So my array has five elements, 10 through to 14, 89 00:04:10,980 --> 00:04:15,980 and I'm going to borrow a slice of the array, zero to three, 90 00:04:17,580 --> 00:04:18,750 but not including three. 91 00:04:18,750 --> 00:04:21,687 So basically elements zero, one and two. 92 00:04:21,687 --> 00:04:25,593 S2 will be elements zero, one and two. 93 00:04:27,540 --> 00:04:30,000 So when I output S2, I output this, 94 00:04:30,000 --> 00:04:31,830 the address of the first element, 95 00:04:31,830 --> 00:04:34,860 the number of elements in the slice 96 00:04:34,860 --> 00:04:37,590 and then the actual values of those elements 97 00:04:37,590 --> 00:04:40,050 output in it using the debug formatter. 98 00:04:40,050 --> 00:04:45,050 So output the start address using the pointer formatter. 99 00:04:45,390 --> 00:04:47,190 And that's here. 100 00:04:47,190 --> 00:04:48,870 Not too interested with that really, 101 00:04:48,870 --> 00:04:50,520 but, you know, might be useful. 102 00:04:50,520 --> 00:04:53,868 The length of the slice in elements, 103 00:04:53,868 --> 00:04:56,610 not bites, but elements. 104 00:04:56,610 --> 00:04:59,103 Okay, so there are three elements in my slice. 105 00:04:59,103 --> 00:05:00,660 That is correct, isn't it? 106 00:05:00,660 --> 00:05:02,850 Zero dot dot three. 107 00:05:02,850 --> 00:05:05,220 And then if you output the whole slice 108 00:05:05,220 --> 00:05:07,080 using the debug formatter, 109 00:05:07,080 --> 00:05:09,600 then it'll output the slice that you've plucked out 110 00:05:09,600 --> 00:05:11,820 that you are viewing into. 111 00:05:11,820 --> 00:05:14,340 Second example here will be exactly the same. 112 00:05:14,340 --> 00:05:16,830 I didn't specify the lower index 113 00:05:16,830 --> 00:05:18,573 so that would default to zero. 114 00:05:20,190 --> 00:05:22,158 In the third L example, 115 00:05:22,158 --> 00:05:25,170 this would give me elements two and three 116 00:05:25,170 --> 00:05:28,860 but not element four, because the upper limit is exclusive. 117 00:05:28,860 --> 00:05:31,377 So element two will be that one, 118 00:05:31,377 --> 00:05:34,410 element three will be that one, 119 00:05:34,410 --> 00:05:36,090 and that's what we see being displayed here. 120 00:05:36,090 --> 00:05:39,780 We have a... our slice has length of two 121 00:05:39,780 --> 00:05:42,120 and the elements are 12 and 13. 122 00:05:42,120 --> 00:05:45,150 There it is a length of two elements, 12 and 13. 123 00:05:45,150 --> 00:05:49,740 Finally starting at position two up to the end. 124 00:05:49,740 --> 00:05:52,410 So that'll give me that slice there. 125 00:05:52,410 --> 00:05:55,710 A length of three elements, 12, 13, 14. 126 00:05:55,710 --> 00:05:59,010 A length of 3, 12, 13, 14. 127 00:05:59,010 --> 00:06:01,710 All right. So basically the same as it is with string slices 128 00:06:01,710 --> 00:06:04,890 except remember that here we're talking about 129 00:06:04,890 --> 00:06:07,380 the actual elements indexing 130 00:06:07,380 --> 00:06:11,373 rather than the bites like we had with a string slice. 131 00:06:12,780 --> 00:06:15,510 Okay. The last example that we're gonna look at 132 00:06:15,510 --> 00:06:17,130 is mutability. 133 00:06:17,130 --> 00:06:18,840 So again, let's discuss the theory 134 00:06:18,840 --> 00:06:21,030 and then we can come back and look at the demo. 135 00:06:21,030 --> 00:06:23,310 So imagine you've got a mutable array. 136 00:06:23,310 --> 00:06:26,340 You've created an array used in the map keyword. 137 00:06:26,340 --> 00:06:28,830 Then you can create a mutable slice 138 00:06:28,830 --> 00:06:30,240 using the ampersand map. 139 00:06:30,240 --> 00:06:31,500 We've seen this a lot now. 140 00:06:31,500 --> 00:06:34,080 You say ampersand map a mutable reference. 141 00:06:34,080 --> 00:06:37,050 A reference which is allowed to mutate the content. 142 00:06:37,050 --> 00:06:40,170 Remember as well, the borrow checker. 143 00:06:40,170 --> 00:06:41,730 The borrow checker will only let you have 144 00:06:41,730 --> 00:06:43,440 one mutable reference at a time. 145 00:06:43,440 --> 00:06:45,210 So bear that in mind as well. 146 00:06:45,210 --> 00:06:49,890 Oh, and then you specify the type of element in the array. 147 00:06:49,890 --> 00:06:51,210 So here's an example. 148 00:06:51,210 --> 00:06:54,570 Here's my mutable array, contains some data. 149 00:06:54,570 --> 00:06:56,610 I take a mutable reference 150 00:06:56,610 --> 00:06:58,200 which will allow me to potentially change, 151 00:06:58,200 --> 00:07:01,140 but I'm only interested in the slice from two to three 152 00:07:01,140 --> 00:07:01,973 really, isn't it? 153 00:07:01,973 --> 00:07:04,830 Because it's exclusive on the upper bound. 154 00:07:04,830 --> 00:07:06,210 This would give me a slice 155 00:07:06,210 --> 00:07:10,650 or that would basically point to this part here. 156 00:07:11,640 --> 00:07:14,370 That's basically what my slice refers to. 157 00:07:14,370 --> 00:07:16,080 And I've explicitly typed it. 158 00:07:16,080 --> 00:07:18,063 I didn't actually need to, but I have, 159 00:07:19,050 --> 00:07:24,050 it is a mutable reference into an array of 32 bit integers. 160 00:07:24,420 --> 00:07:26,610 And it's that particular slice. 161 00:07:26,610 --> 00:07:30,543 I could change element zero in the slice to be 130. 162 00:07:31,410 --> 00:07:36,410 Okay, so 0, 1, 2, this element here would change. 163 00:07:38,100 --> 00:07:39,780 That's element zero. 164 00:07:39,780 --> 00:07:41,703 And that would change to be 130. 165 00:07:42,720 --> 00:07:44,940 Okay, well let's have a look at the example. 166 00:07:44,940 --> 00:07:49,940 Here is the example, and I'm going to run it. 167 00:07:51,750 --> 00:07:53,833 (typing) 168 00:07:55,830 --> 00:07:59,850 Right. So my array originally was 10, 11, 12, 13, 14. 169 00:07:59,850 --> 00:08:02,300 Oh, I've changed an element zero to be a hundred. 170 00:08:03,210 --> 00:08:05,520 I'm gonna print the array afterwards. 171 00:08:05,520 --> 00:08:09,148 Okay, so notice that the array element zero is a hundred. 172 00:08:09,148 --> 00:08:14,148 And then in here, I set myself up a mutable slice, 173 00:08:14,280 --> 00:08:18,000 take a mut borrow mutably that slice. 174 00:08:18,000 --> 00:08:23,000 So elements two and three, that's those elements there. 175 00:08:23,940 --> 00:08:25,560 Those elements are in my slice. 176 00:08:25,560 --> 00:08:29,670 That's what S refers to. S refers to that slice. 177 00:08:29,670 --> 00:08:31,740 Element zero in that slice, 178 00:08:31,740 --> 00:08:34,380 element zero is going to be this one, 179 00:08:34,380 --> 00:08:37,170 that's going to change to be 130. 180 00:08:37,170 --> 00:08:39,450 So when I print the array afterwards, 181 00:08:39,450 --> 00:08:43,777 we see that element zero in the slice has become 130. 182 00:08:44,950 --> 00:08:48,300 Okay? So that's the end of the lesson. 183 00:08:48,300 --> 00:08:51,120 We've talked about the concept of borrowing, 184 00:08:51,120 --> 00:08:53,190 taking a reference to a variable. 185 00:08:53,190 --> 00:08:55,320 We talked about mutable borrowing, 186 00:08:55,320 --> 00:08:57,600 so you can potentially change the value. 187 00:08:57,600 --> 00:08:59,490 We talked about the borrow checker, 188 00:08:59,490 --> 00:09:02,820 very fussy about how many mutable references 189 00:09:02,820 --> 00:09:04,950 and immutable references you can have, 190 00:09:04,950 --> 00:09:06,450 there for your protection. 191 00:09:06,450 --> 00:09:08,550 And then we wrapped up the lesson by looking at 192 00:09:08,550 --> 00:09:12,540 string slices, which is a borrowing of part of the text. 193 00:09:12,540 --> 00:09:14,862 And then we looked at array slices where you can borrow 194 00:09:14,862 --> 00:09:16,710 part of an array. 195 00:09:16,710 --> 00:09:19,410 Okay, so hope you found it interesting. 196 00:09:19,410 --> 00:09:22,530 Quite challenging, but, you know, quite idiomatic, 197 00:09:22,530 --> 00:09:24,333 but very powerful.