-fn first_word(s: &str) -> &str {
- for (i, &item) in s.as_bytes().iter().enumerate() {
- if item == b' ' {
- return &s[..i];
+mod word_utils {
+ pub fn first_word(s: &str) -> &str {
+ for (i, &item) in s.as_bytes().iter().enumerate() {
+ if item == b' ' {
+ return &s[..i];
+ }
}
- }
- s
-}
+ 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)..]));
+ pub 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;
+ return None;
+ }
}
+use word_utils::{first_word, second_word};
+
fn main() {
let s = String::from("Hello world");
println!("first word: '{}'", first_word(&s));