- In this lesson, we're going to look at array slices.
An array slice is similar to a string slice
except that you're peeking into an array
instead of into text.
Okay, so first of all, let's have a quick recap
of what we know about arrays already.
An array is a fixed-size homogeneous collection.
Each element is the same type.
You can create an array using literal syntax,
square brackets containing the values.
Here's an example.
A is an array, contains five values, integers.
The memory for an array is allocated on the stack
and deallocated at the end of the function.
Okay, so it's locally allocated,
doesn't get allocated on the heap,
it's allocated on the stack.
Good to know.
You can create an array slice.
You prefix the array name with an ampersand.
It looks like this.
In this example, my array is here
and my slice s kind of points into the array
and it also kind of knows how big the slice is as well.
We'll talk about that in a bit more detail later on,
but it's like a viewport into the actual data in the array,
either the whole array or part of the array.
If you use this syntax,
then it's a slice of the whole array.
S is basically the whole array content.
So s is an array slice.
It borrows the whole of the array here.
I haven't specified like particular indexes
so it effectively receives the whole of the array.
If you want to, you can explicitly type an array slice
using this syntax ampersand
and then inside square brackets
the type of the elements in the array.
Okay, so here's an example.
Consistent and actually equivalent to the previous slide.
A is my array.
And so a kind of looks like this
and s borrows the array,
or if you like is a slice of the array.
So if you want to explicitly type it,
you'd say it is borrowing an array of 32 bit integers.
Okay, so s is kind of like a slice
or a borrow of the data inside the array.
And by default, it sees the whole of the array
if you write it like that.
Okay, so what information does an array slice contain?
It contains two pieces of information.
The address of an element in the array,
may be the first element,
may be a different element,
and the length of the slice in elements.
So if your array looks like this,
you could have a slice that says
I'm going to point to the first element
and the slice will contain all the elements like so.
Alternatively, you could have a slice
that points to that element
and only has two elements in the slice.
Okay, so you can specify the extent of the array
that you want to view via the slice.
So here we go.
You can access the pointer and the length.
This is similar to what we had with a string slice.
In this example here, I've got an array, five elements.
I have a slice that basically views the whole array.
When I get the address, when I take my slice,
it has an as pointer function.
It'll give me the address of the first element.
I'm gonna say length.
It'll give me the length of the array in elements,
not bites, but elements.
Okay, so it'll give you
the length of the slice here would be five.
Right, well, let's have a look at an example.
We'll have a look at a simple example first,
demo_array_slice_intro.
We'll run the project when we've seen the code.
So here we are then, here's the project in main.
I'm going to uncomment this function call
demo_array_slice_intro.
And it's here
and I'm going to run it.
It's quite straightforward this example actually,
so I'll run it and then we'll just discuss
the output at the end.
cargo run.
Okay, so here's my array.
I can take a slice of the array,
s1 is a slice of the whole array implicitly typed.
And I display the address of the first element,
the length of the slice in elements,
and then the data in the slice, like so.
So it'll output the as pointer,
will give back the address of the first element.
The length, it gives me the length of my slice in elements.
And you can output a slice using the debug formatter.
Remember that, the debug formatter?
You can use that for a slice as well.
So it'll output the elements that you see in your slice,
so s1 was implicitly typed as a reference to an array,
an array slice, in other words.
s2 is explicitly typed as a reference
into a 32 integer array.
So s2, same idea, it's just using explicit type in here.
When I output s2, it's gonna give me the same information.
It's just using explicit type in
rather than implicit type in.
My choice would be to use inferred type in here,
easier on the syntax.
You can write it that way if you want.
Being able to explicitly specify
the type of a slice is very useful if you had a function.
You could have a function that took a slice as a parameter.
So that would be the syntax,
that would be the time when you'd use the explicit syntax
to specify type in as an array,
as a parameter that you pass into a function.
Okay, so that's a simple example of slices.
What we'll do in the next section
is have a look at how to use some more additional techniques
on top of array slices.