]> piware.de Git - learn-rust.git/commitdiff
Put functions into module
authorMartin Pitt <martin@piware.de>
Sun, 22 Aug 2021 13:24:27 +0000 (15:24 +0200)
committerMartin Pitt <martin@piware.de>
Sun, 22 Aug 2021 13:24:27 +0000 (15:24 +0200)
src/main.rs

index 83d94c6cfde002df9a0aca1dc5f34f623bf3634a..7a83cb487326028805912df69daa38a3c2cfa394 100644 (file)
@@ -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));
 fn main() {
     let s = String::from("Hello world");
     println!("first word: '{}'", first_word(&s));