diff --git a/slices/Cargo.lock b/slices/Cargo.lock new file mode 100644 index 0000000..0503983 --- /dev/null +++ b/slices/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "slices" +version = "0.1.0" diff --git a/slices/Cargo.toml b/slices/Cargo.toml new file mode 100644 index 0000000..ffd8810 --- /dev/null +++ b/slices/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "slices" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/slices/src/main.rs b/slices/src/main.rs new file mode 100644 index 0000000..fad7d98 --- /dev/null +++ b/slices/src/main.rs @@ -0,0 +1,33 @@ +fn main() { + let words = String::from("all string of words"); + println!("{words}"); + let first = find_first(&words); + println!("{first}"); + + let second = find_first2(&words); + println!("{second}"); +} + + +fn find_first(words: &String) -> String { + let mut word = String::from(""); + for letter in words.chars() { + if letter == ' ' { + break; + } + word.push_str(&letter.to_string()); + } + word +} + + +fn find_first2(words: &str) -> &str { + let bytes = words.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return &words[..i]; + } + } + &words[..] +} \ No newline at end of file