diff --git a/string_slicing/Cargo.toml b/string_slicing/Cargo.toml new file mode 100644 index 0000000..b1e52e9 --- /dev/null +++ b/string_slicing/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "string_slicing" +version = "0.1.0" +authors = ["laurens "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/string_slicing/src/main.rs b/string_slicing/src/main.rs new file mode 100644 index 0000000..566db74 --- /dev/null +++ b/string_slicing/src/main.rs @@ -0,0 +1,18 @@ +fn first_word(s: &String) -> &str { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return &s[..i] + } + } + + &s[..] +} + +fn main() { + let s = String::from("Hello world"); + + println!("String: {}", s); + println!("First word: {}", first_word(&s)); +}