YouTip LogoYouTip

Rust Slice

# Rust Slice (Slice) Type A slice is a reference to a contiguous sequence of elements in a collection rather than the entire collection. The term "slice" often appears in biology classes, where we take a slice from an organism to observe under a microscope. In Rust, a slice is similar in concept, but it refers to a reference to a part of the data. ### String Slices The simplest and most common type of data slice is the 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 image above explains the principle of string slices (Note: The string type in Rust essentially records the starting position and length of characters in memory. We'll understand this for now). The syntax using `..` to represent a range was introduced in the loops chapter. **x..y** represents the mathematical meaning of **[x, y)**. There can be no operands on either side of `..`: `..y` is equivalent to `0..y` `x..` is equivalent to the position from `x` to the end of the data `..` is equivalent to the position from `0` to the end **Note:** For now, try to avoid using non-English characters in strings due to encoding issues. The specific reasons will be discussed in the "Strings" chapter. The string referenced by a slice is prohibited from having its value changed: ## Example fn main(){ let mut s=String::from(""); 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 prohibited. Actually, by now you might be wondering why we have to write `String::from("")` every time we use a string. Can't we just write **""**? At this point, we must distinguish the difference between these two concepts. In Rust, there are two common string types: `str` and `String`. `str` is a core language type in Rust, which is the string slice we've been discussing in this chapter, often appearing as a reference (`&str`). Any string literal enclosed in double quotes has the type **&str**: let s = "hello"; Here, `s` is a variable of type `&str`. The `String` type is a data type provided by Rust's standard library. It has more functionalityβ€”it supports practical operations like appending and clearing strings. Besides having a starting position property and a length property like `str`, `String` also has a capacity property. Both `String` and `str` support slicing, and the result of a slice is of type `&str`. Note: The result of a slice 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 Besides strings, other linear data structures also support slice 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: 135
← Rust EnumOs Fstat β†’