This is the 25th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Slice type

Slice data types have no ownership, and Slice allows us to refer to a contiguous sequence of elements in a collection without referring to the entire collection. The string slice(string slice) is a reference to a part of a string. As in the following code example, instead of referring to the whole String, we refer to part of the String:

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

    let hello = &s[0.5];
    let world = &s[6.11];
}
Copy the code

A slice can be created using a range specified by [starting_index..ending_index] in brackets, where starting_index is the first position of the slice, Ending_index is the last value of the last slice position. Within it, slice’s data structure stores the slice’s starting position and length, which corresponds to the value of ending_index minus starting_index. So let world = &s[6..11]; In the case of, world will be a pointer to the seventh byte of S (starting from 1) and a slice of length 5. As shown below:

For Rust’s.. The range syntax, if you want to start with the first index (0), does not include values before two dots. Similarly, if slice contains the last byte of the String, the trailing digit can be discarded. The following code:

// Discard the leading digit
fn main() {
    let s = String::from("hello");

    let slice = &s[0.2];
    let slice = &s[..2];
}

// Discard the trailing digits
fn main() {
    let s = String::from("hello");

    let len = s.len();

    let slice = &s[0..len];
    letslice = &s[..] ; }Copy the code

String literals

Now that we know Slice, we can understand string literals correctly. Here s is of type &str: it is a slice pointing to a specific location in the binary. That’s why string literals are immutable; & STR is an immutable reference.

let s = "hello rust";
Copy the code

Other types of slice

The string slice is for strings. However, there are more generic slice types, such as array, which do the same thing as retrieving a portion of a string, or if we want to reference a portion of an array, which does the same thing as the string slice.

let a = [1.2.3.4.5];
let slice = &a[1.3];
Copy the code

The slice variable, of type &[i32], works like the string slice, by storing a reference to the first collection element and a total collection length. You can use this type of slice for all other collections.

conclusion

The ownership system affects the way many other parts of Rust work. The concepts of ownership, borrowing, and slice make Rust programs memory safe at compile time. Rust provides the same way as other system programming languages to control the memory you use, but the ability to automatically erase data owners when they leave scope means you don’t need to write and debug additional control code.

conclusion

The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)