From da5e8d881fe6e659d168b7ace5ab9af35541a613 Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Sun, 22 Aug 2021 15:24:27 +0200 Subject: [PATCH] Put functions into module --- src/main.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) 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)); -- 2.39.5