kota's memex

Slices are not "resizable arrays" like in go. For that you'll want the rust vectors. It's better to think of a slice as a particular view into another existing collection. You can have a mutable slice, but you cannot append or remove to a slice.

Slices let you reference a contiguous sequence of elements in a collection rather than the whole collection. A slice is a kind of reference, so it does not have ownership.

// Create an array.
let a = [1, 2, 3, 4, 5];

// Make a slice of that array.
let slice = &a[1..3];
assert_eq!(slice, &[2, 3]);

string slices

string slices

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

let hello = &s[0..5];
let world = &s[6..11];

Rather than a reference to the entire String, hello is a reference to a portion of the String. If you want to start the index at 0, you can just drop the value [..2] or to go to the end of the string drop the ending value [2..].

Note: String slice range indices must occur at valid UTF-8 character boundaries. If you attempt to create a string slice in the middle of a multibyte character, your program will panic. In real code you should handle UTF-8 characters properly!