Rust Slice Type

A slice is a partial reference to a data value.

The name "slice" often appears in biology class. When making sample slides, we need to take slices from an organism for observation under a microscope. In Rust, a slice means roughly the same thing, except that it takes a reference from data.

String Slice

The simplest and most commonly used data slice type is the string slice (String Slice).

Example

fn main() {
    let s = String::from("broadcast");

    let part1 = &s[0..5];
    let part2 = &s[5..9];

    println!("{}={}+{}", s, part1, part2);
}

Output:

broadcast=broad+cast

The above diagram explains the principle of string slicing (Note: The string type in Rust essentially records the starting position and length of characters in memory; we only need to understand this for now).

Usage..The syntax for expressing ranges has appeared in the loop chapter.x..yrepresents[x, y)the mathematical meaning of...Both sides may omit the operand:

..y 等价于 0..y
x.. 等价于位置 x 到数据结束
.. 等价于位置 0 到结束

Note:For now, try not to use non-English characters in strings, due to encoding issues. The specific reasons will be explained in the "String" chapter.

The string referenced by a slice is forbidden from having its value changed:

Example

fn main() {
    let mut s = String::from("example");
    let slice = &s[0..3];
    s.push_str("yes!"); // Error
    println!("slice = {}", slice);
}

This program is incorrect.

s is partially referenced, so changing its value is forbidden.

Actually, by now you must be wondering why every time we use a string, we have to write it like thisString::from("example"), and just write"example"wouldn't that work?

At this point, we must distinguish the conceptual difference between these two. In Rust, there are two commonly used string types: str and String. str is a Rust core language type, which is the string slice (String Slice) discussed throughout this chapter, and it often appears in the form of a reference (&str).

Any string constant enclosed in double quotes has the overall type of&str:

let s = "hello";

Here, s is a variable of type &str.

The String type is a data type provided by the Rust standard public library, and its functionality is more complete — it supports practical operations such as string appending and clearing. In addition to the character start position attribute and string length attribute that both String and str share, String also has a capacity attribute.

Both String and str support slicing, and the result of slicing is data of type &str.

Note: The slice result must be a reference type, but the developer must explicitly indicate this:

let slice = &s[0..3];

There is a quick way to convert String to &str:

let s1 = String::from("hello");
let s2 = &s1[..];

Non-String Slice

In addition to strings, some other linear data structures also support slicing operations, such as arrays:

Example

fn main() {
    let arr = [1, 3, 5, 7, 9];
    let part = &arr[0..3];
    for i in part.iter() {
        println!("{}", i);
    }
}

Output:

1
3
5
Other Extensions