1 fn first_word(s: &str) -> &str {
2 for (i, &item) in s.as_bytes().iter().enumerate() {
11 fn second_word(s: &str) -> Option<&str> {
12 for (i, &item) in s.as_bytes().iter().enumerate() {
14 return Some(first_word(&s[(i + 1)..]));
22 let s = String::from("Hello world");
23 println!("first word: '{}'", first_word(&s));
24 println!("second word: '{}'", second_word(&s).unwrap());
26 let s2 = "hello dude blah";
27 println!("second word of single: '{}'", second_word(s2).unwrap_or("(none)"));
29 match second_word(s2) {
30 Some(w) => println!("match: second word of '{}' exists: {}", s2, w),
31 None => println!("match: second word of '{}' does not exist", s2),