Commit a47a3595 authored by Chris's avatar Chris
Browse files

[the-slice-type] added content

parent 76db7a33
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "the-slice-type"
version = "0.1.0"
+6 −0
Original line number Diff line number Diff line
[package]
name = "the-slice-type"
version = "0.1.0"
edition = "2024"

[dependencies]
+83 −0
Original line number Diff line number Diff line
fn main() {
    let mut s = String::from("hello world!");
    let first = first_word(&s);
    println!("The first word's ending index is: {}", first);
    println!("you can also consider this to be the length of the first word");

    s.clear();

    // here's how slices work
    s = String::from("hello world!");
    let hello: &str = &s[0..5]; // this is how you declare range from a string
    println!("{hello}");
    s.push_str(" (again)");

    let _slice = &s[..2]; // this is the same as [0..2]
    let _slice = &s[0..2]; // this is the same as [..2]
    let len = s.len();
    let _slice = &s[3..len]; // this is the same as [3..]
    let _slice = &s[3..]; // this is the same as [3..len]

    // take a slice of the entire string lol
    let _slice = &s[..];

    // note that the "str" type is string slice, and you can use this type or the String type
    let my_string = String::from("hello world");
    // `first_word` works on slices of `String`s, whether partial or whole.
    let _word = first_word(&my_string[0..6]);
    let _word = first_word(&my_string[..]);
    // `first_word` also works on references to `String`s, which are equivalent
    // to whole slices of `String`s.
    let _word = first_word(&my_string);

    let my_string_literal = "hello world";

    // `first_word` works on slices of string literals, whether partial or
    // whole.
    let _word = first_word(&my_string_literal[0..6]);
    let _word = first_word(&my_string_literal[..]);

    // Because string literals *are* string slices already,
    // this works too, without the slice syntax!
    let word = first_word(my_string_literal);

    println!("the word variable reads: {word}");

    // you can do slices on things that aren't strings, too:
    let a = [1, 2, 3, 4, 5];
    let slice = &a[1..3];
    assert_eq!(slice, &[2, 3]);

    // finally, you can see that the str type is more memory hungry than the String type:
    let s = String::from("hello");
    let _s2: &String = &s;
    let _s3: &str = &s[..];
    println!("&String={} &str={}"
        std::mem::size_of::<&String>(),
        std::mem::size_of::<&str>()
    );
}

// this is an approach to implement first_word() without string slices
// fn first_word(s: &String) -> usize {
//     let bytes = s.as_bytes();
//
//     for (i, &item) in bytes.iter().enumerate() {
//         if item == b' ' {
//             return i;
//         }
//     }
//
//     s.len()
// }

// this is first_word with string slices
fn first_word(s: &str) -> &str {
    let bytes = s.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i];
        }
    }
    &s[..]
}