String Slicing#
String Slicing Syntax#
Rust provides convenient syntax for slicing strings.
Let’s explore this through the following code.
fn main() {
let s = String::from("hello world");
println!("{}", &s[0..5]);
println!("{}", &s[6..11]);
println!("{}", &s[..5]);
println!("{}", &s[6..]);
println!("{}", &s[..]);
}By coding &s[0..5], you can slice out "hello".
Here, 0 means the start index, and 5 means the end index + 1.
(Note that you use the end index + 1.)
By coding &s[6..11], you can slice out "world".