The Slice Type

Posted by ChaosNyaruko on March 15, 2021

Concept

A partial reference of a contiguous sequence of elements in a collection.

  • contiguous
  • a data type that does not have ownership

Usage

String Slices

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

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

string slice referring to part of a String

Like others languages, we can simpify the syntax if we want to start at the first index(zero) or include the last byte of String


#![allow(unused)]
fn main() {
let s = String::from("hello");

let slice = &s[0..2];
let slice = &s[..2];

}


#![allow(unused)]
fn main() {
let s = String::from("hello");

let len = s.len();

let slice = &s[3..len];
let slice = &s[3..];

}


#![allow(unused)]
fn main() {
let s = String::from("hello");

let len = s.len();

let slice = &s[0..len];
let slice = &s[..];

}

Attention

  • String slice range indices must occur at valid UTF-8 character boundaries.
  • String literals are slices
  • Using string slices as parameters usually make our API more general and useful without losing any functionality.