English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Rust Slice (Slice) Type

Slice (Slice) is a partial reference to a data value.

The name 'slice' often appears in biology class, when we make sample slides, we need to take slices from the organism for observation under the microscope. In Rust, the meaning of slicing is roughly the same, but it refers to the data it takes from.

String Slice

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

fn main() {
    let s = String::from("broadcast");
    let part1 = &s[0..5];
    let part2 = &s[59];
    println!("{}={}+{}", s, part1, part2);
}

Running Result:

broadcast=broad+cast

The figure above explains the principle of string slicing (note: in Rust, the string type actually records the starting position and length of the characters in memory, and we will understand this temporarily).

The syntax of using '...' to represent a range has appeared in the loop section.x..y represents [x, y) The mathematical meaning of the '...' operator. Both sides can be without operands:

..y is equivalent to 0..y
x.. is equivalent to position x to the end of the data
.. is equivalent to position 0 to the end

Note:So far, try not to use non-English characters in strings because of encoding issues. The specific reasons will be described in the "String" section.

The value of the string referenced by the slice is prohibited from being changed:

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

This program is incorrect.

Part of 's' is quoted and its value is prohibited from being changed.

In fact, by now, you must be wondering why you have to write String::from("w")) every time you use a string3codebox) directly write "w3codebox" Can't it?

So far, we must distinguish the difference between the two concepts. 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) mentioned in this chapter, and often appears in the form of a reference (&str).

All string literals enclosed in double quotes have the following type properties: &str:

let s = "hello";

Here, s is a &str type variable.

The String type is a data type provided by the Rust standard public library, which has more comprehensive functions—it supports practical operations such as string concatenation and clearing. In addition to having a character starting position attribute and a string length attribute like str, String also has a capacity (capacity) attribute.

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

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 a String to &str:

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

Non-string Slices

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

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

Running Result:

1
3
5