A slice is a reference to a part of an array-like structure (e.g.:
array, Vec, String).
Table 1:Array and slice types
Type | Description |
|---|---|
| An array of |
| An array of |
| A slice: a reference on part of an array of |
Slices are constructed using the reference operator &, the array
descriptor [] and a range (.. operator). The range (see
Ranges) describes the part of the array-like object to
reference. Here are some examples of slices of the same object a:
&a[0..2]; // From start to index 2 excluded.
&a[..=2]; // From start to index 2 included.
&a[2..]; // From index 2 to end.
&a[2..2]; // Empty slice.
&a[..]; // All elements.19.3.1Array slices¶
Array slices have type &[T], and can be constructed from any array using
the ampersand operator and a range. Here are some examples of array
slices:
let a = [-10i16, 4, 8, -4, 12];
println!("Slice [1..3]: {:?}", &a[1..3]);
println!("Slice [3..3]: {:?}", &a[3..3]);
println!("Slice [2..]: {:?}", &a[2..]);
println!("Slice [..2]: {:?}", &a[..2]);
println!("Slice [..]: {:?}", &a[..]);Slice [1..3]: [4, 8]
Slice [3..3]: []
Slice [2..]: [8, -4, 12]
Slice [..2]: [-10, 4]
Slice [..]: [-10, 4, 8, -4, 12]
Array slices can be taken from Vec objects too:
let v = vec![-33i8, 5, -14, 10, 55];
println!("Slice [1..3]: {:?}", &v[1..3]);Couldn't automatically determine type of variable `v`.
Please give it an explicit type.Slices from an array or a Vec object have exactly the same type &[T].
This means we can write functions that accept an array slice, and we will
be able to run it on either a vector or an array. In Rust, functions that
need to access a vector or an array accept instead a slice in order to be
versatile. Example:
fn foo(x: &[i16]) {
println!("Slice received by foo: {:?}", x);
}
let a = [-10i16, 4, 8, -4, 12];
let v = vec![-33i16, 5, -14, 10, 55];
foo(&a[1..3]);
foo(&v[..4]);Table 2:Some methods of slice
Method | Description |
|---|---|
| Returns |
| Test the end of the slice. |
| Get a slice of the slice. |
| Get a mutable slice of the slice. |
| Return the length of the |
| Build a new slice by repeating the slice n times. |
| Split a slice in sub-slices, using a predicate function to know where to cut. |
| Test if a string starts with a pattern. |
19.3.2String slices¶
The str type
represents an unsized UTF-8 string (see UTF-8). The
characters are stored as a sequence of bytes. The str type is thus a
[u8] unsize type with a UTF-8 validation layer.
Like any [T] unsize type, str is not usable as such, but only as a
slice. As for other slices, &str is a fat pointer that stores a
pointer to the start of the string and the length of the string. The
length is only known at runtime.
19.3.2.1Defining a string slice¶
We may define a string slice using a literal string:
let s = "abcdef";
sA literal string is itself a string slice, with a static lifetime. It is stored inside the compiled program and hence, when loaded, resides inside the read-only memory of the process (i.e.: where the code and constant data reside).
Here is the same definition as above with the type made explicit:
let s: &'static str = "abcdef";
s19.3.2.2Passing strings to functions¶
The following function accepts only a reference to a String object:
fn foo(s: &String) {
println!("s: {s}");
}
let my_string = String::from("ABC");
foo(&my_string);While the following function accepts a string slice, which allows us to
use either a String object or a slice on a string literal. The function
will not see the difference:
fn foo(s: &str) {
println!("s: {s}");
}
let s1 = "ABC";
let s2 = String::from("DEF");
foo(&s1);
foo(&s2);19.3.2.3str methods¶
Table 3:Some methods of str
Method | Description |
|---|---|
| Get an iterator on the bytes. |
| Get an iterator on the characters. |
| Search forward for a pattern. |
| Convert a slice of bytes into a string slice. |
| Replace all matches of the pattern with the string s. |
| Search backward for a pattern. |
| Convert the string into lowercase. |
| Convert to a |
| Convert the string into uppercase. |
| Remove white spaces at both ends of the string. |