Rust - string slices



examples/strings/slice/src/main.rs
fn main() {
    let text = "The black cat climbed the green tree!";
    println!("{}", text);
    println!("'{}'", &text[4..4]);
    println!("'{}'", &text[4..=4]);
    println!("'{}'", &text[4..5]);
    println!("'{}'", &text[4..9]);
    println!("'{}'", &text[4..=8]);
    println!("'{}'", &text[4..]);
    println!("'{}'", &text[4..text.len()]);
    println!("'{}'", &text[4..text.len()-1]);
    println!("'{}'", &text[..4]);

    println!("'{}'", &text[4..40]);  // thread 'main' panicked at 'byte index 40 is out of bounds
}

The black cat climbed the green tree!
''
'b'
'b'
'black'
'black'
'black cat climbed the green tree!'
'black cat climbed the green tree!'
'black cat climbed the green tree'
'The '
thread 'main' panicked at 'byte index 40 is out of bounds of `The black cat climbed the green tree!`', examples/strings/slice.rs:14:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace