From 824fd4371bafc487163edf33cb58b05fc4b22971 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Sun, 22 Aug 2021 15:31:47 +0200 Subject: [PATCH] Move word_utils to separate file --- src/main.rs | 22 +--------------------- src/word_utils.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 src/word_utils.rs 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; +} -- 2.39.5