slices
This commit is contained in:
parent
54b7b3e4f8
commit
389d2fc0aa
7
slices/Cargo.lock
generated
Normal file
7
slices/Cargo.lock
generated
Normal 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
8
slices/Cargo.toml
Normal 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
33
slices/src/main.rs
Normal 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[..]
|
||||
}
|
Loading…
Reference in New Issue
Block a user