Hi,
I have this code following, I wonder why I can pass a &Vec to &[i32] and also what's the difference between these two.
I just started learning rust and I would be so happy if you can help me. Thanks!
Can you explain more deeply? In my limited knowledge, [i32] means an array and Vec means an vector, they are different, &[i32] and &Vec are just references of the two. From what you said, I guess &Vec is simply the reference of Vec. Maybe you can teach me what's &[i32], why it's not simply the reference of the array [i32]? Thank you!
No, &[i32] means a slice (typically always written with ampersand). The thing called an array includes the size, e.g. [i32; 10] is an array of length 10.
Whenever you see the &[i32] type, it behaves as if you had the following struct:
// This is not quite valid syntax, just to illustrate.
struct &[i32] {
first_element: *const i32,
length: usize,
}
That is, it has a pointer to the first element and the length. To access the nth element, you take the memory location of the first element and add the index to it, which yields a pointer to the nth element.
It is possible to create a slice to anything that stores multiple elements sequentially in memory. For example:
You can get an &[i32] that points to the entire contents of an Vec<i32>. This would be a pointer to the first element and use the vector's length as the length.
You can get an &[i32] that points to a subset of a Vec<i32> with &the_vector[4..10]. This would be a pointer to the fifth element in the vector (starts from zero!) and the length would be six.
Many other types store several elements in a sequence. E.g. you can get a slice that points into an array.
You can create empty slices of length zero by writing &[].
You can even create slices of a single item using std::slice::from_ref. This always results in a length of one.