finally finished slice...

This commit is contained in:
2022-12-20 09:14:10 -05:00
parent 9bca80091f
commit 8f32bb41ba
3 changed files with 59 additions and 3 deletions

View File

@ -1,4 +1,4 @@
fn first_word(s: &String) -> usize {
fn first_word_int(s: &str) -> usize {
let b = s.as_bytes();
for (i, &val) in b.iter().enumerate() {
if val == b' ' {
@ -7,7 +7,23 @@ fn first_word(s: &String) -> usize {
}
s.len()
}
fn main() {
println!("{}", first_word(&String::from("sadf sdfsd fesf")));
fn first_word(s: &str) -> &str {
let b = s.as_bytes();
for (i, &val) in b.iter().enumerate() {
if val == b' ' {
return &s[..i];
}
}
&s[..]
}
fn main() {
let test_string = "oh my gerd";
let string = String::from("sdf sdf esv gs ");
let slice = &string[0..first_word_int(&string)];
println!("here is the first word: _{}_", slice);
println!("here is the first word: _{}_", first_word(&string));
println!("here is the first word: _{}_", first_word(test_string));
println!("{}", test_string);
}