usize version of first_word
This commit is contained in:
parent
2c6ad398f1
commit
e5b04eb8c4
@ -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…
Reference in New Issue
Block a user