X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=src%2Fword_utils.rs;fp=src%2Fword_utils.rs;h=0000000000000000000000000000000000000000;hp=e3e71bf135ae753c218a2dc72d10bfee2d425f85;hb=b653e9fe9d8492f2d4b5419721f7e00b392055f9;hpb=8c240f514e17c9a935f5f392e6a87779ae1c09e8 diff --git a/src/word_utils.rs b/src/word_utils.rs deleted file mode 100644 index e3e71bf..0000000 --- a/src/word_utils.rs +++ /dev/null @@ -1,43 +0,0 @@ -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)..])); - } - } - - None -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_first_word() { - assert_eq!(first_word(""), ""); - assert_eq!(first_word("one"), "one"); - assert_eq!(first_word("one two"), "one"); - - assert_eq!(first_word(&String::from("one two")), "one"); - } - - #[test] - fn test_second_word() { - assert_eq!(second_word(""), None); - assert_eq!(second_word("one"), None); - assert_eq!(second_word("one two"), Some("two")); - assert_eq!(second_word("one two three"), Some("two")); - - assert_eq!(second_word(&String::from("one two three")), Some("two")); - } -}