usize version of first_word

cat-town
Dan Buch 6 years ago
parent 2c6ad398f1
commit e5b04eb8c4
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7

@ -1,11 +1,18 @@
fn main() {
let s1 = String::from("hello");
let s = String::from("what the what");
let w = first_word(&s);
let len = calculate_length(&s1);
println!("The length of '{}' is {}.", s1, len);
println!("w={}", w);
}
fn calculate_length(s: &String) -> usize {
fn first_word(s: &String) -> usize {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return i;
}
}
s.len()
}

Loading…
Cancel
Save