This commit is contained in:
Tyrel Souza 2023-02-26 23:03:42 -05:00
parent 54b7b3e4f8
commit 389d2fc0aa
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E
3 changed files with 48 additions and 0 deletions

7
slices/Cargo.lock generated Normal file
View File

@ -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"

8
slices/Cargo.toml Normal file
View File

@ -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]

33
slices/src/main.rs Normal file
View File

@ -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[..]
}