Rust - string slices
- Provide address of bytes, but make sure you are on the character boundaries!
examples/strings/slice/src/main.rs
fn main() { let text = String::from("The black cat: πβ climbed the green tree: π³!"); println!("{}", text); println!("'{}'", &text[4..4]); // '' empty string println!("'{}'", &text[4..=4]); // 'b' println!("'{}'", &text[4..9]); // 'black' println!("'{}'", &text[30..]); // ' the green tree: π³!' println!("'{}'", &text[..4]); // 'The ' println!("'{}'", &text[15..22]); // 'πβ' //println!("'{}'", &text[16..22]); // panic!: byte index 16 is not a char boundary; it is inside 'π' (bytes 15..19) //println!("'{}'", &text[25..60]); // panic! at 'byte index 60 is out of bounds }
The black cat: πβ climbed the green tree: π³! '' 'b' 'black' ' the green tree: π³!' 'The ' 'πβ'