I'm a bit lost on the concept of slices in rust. I know that [T] is a contiguous block of elements of type T with a unknown size and we need a pointer to that data to access it. What I don't understand is if we run the following code:
let slice: [u8] = arr[1..3];
Is the compiler trying to access the first and second element from 'arr' and storing it in the stack, but it can't do that since it does not know the size, therefore we are forced to refer the first and second element from 'arr' using a pointer like this:
let slice: [u8] = &arr[1..3];
Secondly, I don't get what it means when the documentation says that slices are a view onto a contiguous block of memory
How is str
(a slice type) a view onto a contiguous block of memory? Isn't &str
(a reference to a slice) a view onto a contiguous block of memory?
I come from a C++ background so it is a bit odd seeing how string and slices work in rust