The Rust Slice Type
Slice is a kind of data structure that store heap memory, it contains two fields, one representing the memory address and the other representing the length. Our most common string literal is a kind of slice.
Slice let us reference a contiguous sequence of elements in a collection rather than the whole collection. A slice is a kind of reference, so it does not have ownership. And the slice ensures that the data it references is always valid.
String Slices
A string slice is a reference to part of a String, and it looks like this:
1 | let s = String::from("hello world"); |
The s is a reference to the entire String hello world
, but the world is a slice, it reference to part of the String hello world
. String slice referring to part of a String
We can use slices to refer to any part of a string with Rust’s ..
syntax, for example:
1 | let s = String::from("hello world"); |
The string slice type is represented by &str
, it can be used as a function return type, for example:
1 | fn first_world(s:&String) -> &str { |
Other Slices
In addition to string slicing, there are other collection slicing types, such as array slicing.
1 | let a:[i8;4]=[2, 3, 4, 5]; ==> &[i8] |