Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

[T,N]

An array of T elements, of fixed size N known at compile-time. Stored on the stack.

[T]

An array of T elements, of unknown size. This type is never used as-is.

&[T]

A slice: a reference on part of an array of T elements, of size known at runtime. The referenced array may be stored anywhere.

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:

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:

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:

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:

Table 2:Some methods of slice

Method

Description

contains(x)

Returns true if the element x is inside the slice.

ends_with(sub_slice)

Test the end of the slice.

get(range)

Get a slice of the slice.

get_mut(range)

Get a mutable slice of the slice.

len()

Return the length of the str object (i.e.: the number of elements).

repeat(n)

Build a new slice by repeating the slice n times.

split(predicate)

Split a slice in sub-slices, using a predicate function to know where to cut.

starts_with(sub_slice)

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:

A 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:

19.3.2.2Passing strings to functions

The following function accepts only a reference to a String object:

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:

19.3.2.3str methods

Table 3:Some methods of str

Method

Description

bytes()

Get an iterator on the bytes.

chars()

Get an iterator on the characters.

find(pattern)

Search forward for a pattern.

from_utf8(bytes)

Convert a slice of bytes into a string slice.

replace(pattern, s)

Replace all matches of the pattern with the string s.

rfind(pattern)

Search backward for a pattern.

to_lowercase()

Convert the string into lowercase.

to_string()

Convert to a String object.

to_uppercase()

Convert the string into uppercase.

trim()

Remove white spaces at both ends of the string.

see solution

see solution

see solution