- If you've created a slice into an array,
you can iterate over the elements in the slice.
No great surprise there.
The slice knows the address of the element,
the first element that it refers to,
it knows the number of elements in the slice,
it might be the whole array or a portion of the array.
and you can iterate over that slice of the data.
So in this example here, my array contains five elements.
10, 11, 12, 13, 14, S is a reference to the array.
In other words, it's a slice to the whole array.
I can iterate over the elements in my slice.
In this case it would be equivalent
to iterating over the whole array.
My slice represents the whole array.
I'm iterating over the elements in the slice.
Remember what's happening under the covers
is that the array would look like this,
10, 11, 12, 13, 14,
and the slice, S, will have a pointer
that points to the first element
and the length here is going to be the whole array.
So when you iterate over a slice,
basically what you're doing is you are iterating
over this portion of the array,
which in this case is the whole array,
10, 11, 12, 13, and 14.
Okay? So obviously the point being
that you could choose a sub array if you wanted to.
Anyway, let's have a quick look at this example
before we get any any further.
So back into the demo project, lesson seven: borrowing.
In my main RS, I'm going to uncomment.
This is the last demo for this lesson.
Demo array slice techniques.
And it's here.
And I've got three functions looking at how to
iterate over a slice, how to get part of an array,
and how to mutate an array via a slice.
So those three functions we're gonna look at
in the next few minutes.
We'll first of all, look at slice iteration
and we'll just ignore these other two functions,
just for now.
Let's have a look at slice iteration.
Quite straightforward. Same as the slide, isn't it?
We've got my array.
I've got a slice over the whole array,
and the elements in the slice,
I can iterate over the elements in the slice
and it will be as if I'm iterating over the whole array.
It'll output all the elements,
10, 11, 12, 13, 14 when I do a cargo run.
So those are the elements in the slice.
10, 11, 12, 13, 14.
Fantastic. So that was easy enough.
Let's just collapse that one down.
What we'll do next is we'll have a look at
how to slice over part of an array.
So let's discuss the theory first.
So you can create a slice as a portion of an array
exactly the same as the the way it works
with a string slice,
except that with an array,
you're always specifying the element numbers.
With a string, it was the bite position,
but with an array, the slice elements,
the indexes are the actual elements themselves.
So when you create a slice, you can specify the start
and the end index as element positions.
The start index is inclusive, starts at zero by default,
the end index is exclusive.
And if you don't specify the end index
then it defaults to the end of the whole array,
what you'd expect.
We do the same idea as you had with string slices earlier.
So in this example, here's my array, A,
and you could specify,
basically you're borrowing a slice of the array
starting at whatever start index you want, inclusive,
dot dot and then the exclusive end index.
So let's see an example of this.
Okay, so here we are.
I'm going to look at the slice part of array and it's here.
So I'll let you look at the code
while I'm doing a cargo run.
(typing)
So my array has five elements, 10 through to 14,
and I'm going to borrow a slice of the array, zero to three,
but not including three.
So basically elements zero, one and two.
S2 will be elements zero, one and two.
So when I output S2, I output this,
the address of the first element,
the number of elements in the slice
and then the actual values of those elements
output in it using the debug formatter.
So output the start address using the pointer formatter.
And that's here.
Not too interested with that really,
but, you know, might be useful.
The length of the slice in elements,
not bites, but elements.
Okay, so there are three elements in my slice.
That is correct, isn't it?
Zero dot dot three.
And then if you output the whole slice
using the debug formatter,
then it'll output the slice that you've plucked out
that you are viewing into.
Second example here will be exactly the same.
I didn't specify the lower index
so that would default to zero.
In the third L example,
this would give me elements two and three
but not element four, because the upper limit is exclusive.
So element two will be that one,
element three will be that one,
and that's what we see being displayed here.
We have a... our slice has length of two
and the elements are 12 and 13.
There it is a length of two elements, 12 and 13.
Finally starting at position two up to the end.
So that'll give me that slice there.
A length of three elements, 12, 13, 14.
A length of 3, 12, 13, 14.
All right. So basically the same as it is with string slices
except remember that here we're talking about
the actual elements indexing
rather than the bites like we had with a string slice.
Okay. The last example that we're gonna look at
is mutability.
So again, let's discuss the theory
and then we can come back and look at the demo.
So imagine you've got a mutable array.
You've created an array used in the map keyword.
Then you can create a mutable slice
using the ampersand map.
We've seen this a lot now.
You say ampersand map a mutable reference.
A reference which is allowed to mutate the content.
Remember as well, the borrow checker.
The borrow checker will only let you have
one mutable reference at a time.
So bear that in mind as well.
Oh, and then you specify the type of element in the array.
So here's an example.
Here's my mutable array, contains some data.
I take a mutable reference
which will allow me to potentially change,
but I'm only interested in the slice from two to three
really, isn't it?
Because it's exclusive on the upper bound.
This would give me a slice
or that would basically point to this part here.
That's basically what my slice refers to.
And I've explicitly typed it.
I didn't actually need to, but I have,
it is a mutable reference into an array of 32 bit integers.
And it's that particular slice.
I could change element zero in the slice to be 130.
Okay, so 0, 1, 2, this element here would change.
That's element zero.
And that would change to be 130.
Okay, well let's have a look at the example.
Here is the example, and I'm going to run it.
(typing)
Right. So my array originally was 10, 11, 12, 13, 14.
Oh, I've changed an element zero to be a hundred.
I'm gonna print the array afterwards.
Okay, so notice that the array element zero is a hundred.
And then in here, I set myself up a mutable slice,
take a mut borrow mutably that slice.
So elements two and three, that's those elements there.
Those elements are in my slice.
That's what S refers to. S refers to that slice.
Element zero in that slice,
element zero is going to be this one,
that's going to change to be 130.
So when I print the array afterwards,
we see that element zero in the slice has become 130.
Okay? So that's the end of the lesson.
We've talked about the concept of borrowing,
taking a reference to a variable.
We talked about mutable borrowing,
so you can potentially change the value.
We talked about the borrow checker,
very fussy about how many mutable references
and immutable references you can have,
there for your protection.
And then we wrapped up the lesson by looking at
string slices, which is a borrowing of part of the text.
And then we looked at array slices where you can borrow
part of an array.
Okay, so hope you found it interesting.
Quite challenging, but, you know, quite idiomatic,
but very powerful.