+++ /dev/null
-fn first_word(s: &str) -> &str {
- for (i, &item) in s.as_bytes().iter().enumerate() {
- if item == b' ' {
- return &s[..i];
- }
- }
-
- s
-}
-
-fn second_word(s: &str) -> Option<&str> {
- for (i, &item) in s.as_bytes().iter().enumerate() {
- if item == b' ' {
- return Some(first_word(&s[(i + 1)..]));
- }
- }
-
- return None;
-}
-
-fn main() {
- let s = String::from("Hello world");
- println!("first word: '{}'", first_word(&s));
- println!("second word: '{}'", second_word(&s).unwrap());
-
- let s2 = "hello dude blah";
- println!("second word of single: '{}'", second_word(s2).unwrap_or("(none)"));
-
- match second_word(s2) {
- Some(w) => println!("match: second word of '{}' exists: {}", s2, w),
- None => println!("match: second word of '{}' does not exist", s2),
- }
-}