From: Martin Pitt Date: Sun, 22 Aug 2021 13:24:27 +0000 (+0200) Subject: Put functions into module X-Git-Url: https://piware.de/gitweb/?a=commitdiff_plain;h=da5e8d881fe6e659d168b7ace5ab9af35541a613;p=learn-rust.git Put functions into module --- diff --git a/src/main.rs b/src/main.rs index 83d94c6..7a83cb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,27 @@ -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));