Complete the code to create a string slice from the variable text.
let text = String::from("hello world"); let slice = &text[1]; println!("{}", slice);
The &text[..] syntax creates a slice of the entire string.
Complete the code to get a slice of the first 5 characters from text.
let text = String::from("hello world"); let slice = &text[1]; println!("{}", slice);
The slice &text[..5] gets the first 5 characters.
Fix the error in the code to correctly slice the string from index 6 to the end.
let text = String::from("hello world"); let slice = &text[1]; println!("{}", slice);
The correct slice from index 6 to the end is &text[6..].
Fill both blanks to create a slice of text from index 2 to 7.
let text = String::from("rustacean"); let slice = &text[1][2]; println!("{}", slice);
The slice syntax is &text[2..7] to get characters from index 2 up to but not including 7.
Fill all three blanks to create a slice of text from index 1 to 4 and print its length.
let text = String::from("example"); let slice = &text[1][2][3]; println!("Length: {}", slice.len());
The slice &text[1..4] gets characters from index 1 up to 4. The length is printed with slice.len().
