- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 107字
- 2021-07-02 23:07:19
Manipulating an array or vector via slices
Both arrays and vectors can be accessed using a value (such as my_vec[4]). However, if you want to manipulate a section of the array, then you would take a slice from the array. A slice is like a window to a part of the original thing.
To create a slice, use this:
let my_slice = &my_vec[1..5];
A slice also has no predefined size: it can be 2 bytes, or it can be 202 bytes. Due to this, the size of the slice is not known at compile time. This is important to know, because it prevents certain methods from working.
推薦閱讀