usize version of first_word

This commit is contained in:
Dan Buch 2017-12-30 21:48:50 -05:00
parent 2c6ad398f1
commit e5b04eb8c4
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7

View File

@ -1,11 +1,18 @@
fn main() { 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!("w={}", w);
println!("The length of '{}' is {}.", s1, len);
} }
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() s.len()
} }