APIs

  1. from: Create a string from a string literal. e.g.: let s = String::from("hello");
  2. push: appends a literal to a string. e.g.: s.push_str(", world!");.
  3. as_bytes: Converts String into an array of bytes.
  4. slice: With Rust’s .. range syntax, if you want to start at index 0, you can drop the value before the two periods. In other words, these are equal: let hello = &s[0..5]; and let hello = &s[..5]. By the same token, if your slice includes the last byte of the String, you can drop the trailing number. You can also drop both values to take a slice of the entire string.

    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 exit with an error. For the purposes of introducing string slices, we are assuming ASCII only in this section; a more thorough discussion of UTF-8 handling is in the “Storing UTF-8 Encoded Text with Strings” section of Chapter 8.