From 389d2fc0aace8989e15d895711946cff5d8d9d8f Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Sun, 26 Feb 2023 23:03:42 -0500 Subject: [PATCH] slices --- slices/Cargo.lock | 7 +++++++ slices/Cargo.toml | 8 ++++++++ slices/src/main.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 slices/Cargo.lock create mode 100644 slices/Cargo.toml create mode 100644 slices/src/main.rs 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