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