From: Martin Pitt Date: Sun, 22 Aug 2021 13:31:47 +0000 (+0200) Subject: Move word_utils to separate file X-Git-Url: https://piware.de/gitweb/?a=commitdiff_plain;h=824fd4371bafc487163edf33cb58b05fc4b22971;p=learn-rust.git Move word_utils to separate file --- diff --git a/src/main.rs b/src/main.rs index 7a83cb4..058f858 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,4 @@ -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 - } - - 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; - } -} +mod word_utils; use word_utils::{first_word, second_word}; diff --git a/src/word_utils.rs b/src/word_utils.rs new file mode 100644 index 0000000..d087820 --- /dev/null +++ b/src/word_utils.rs @@ -0,0 +1,19 @@ +pub fn first_word(s: &str) -> &str { + for (i, &item) in s.as_bytes().iter().enumerate() { + if item == b' ' { + return &s[..i]; + } + } + + s +} + +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; +}