From bb5741755e5cc9a567f5eae28d9dc03554053cb8 Mon Sep 17 00:00:00 2001 From: Bauke Date: Wed, 6 Apr 2022 16:20:49 +0200 Subject: [PATCH] Solve implement-strstr. --- source/implement_strstr/mod.rs | 14 ++++++++++++++ source/lib.rs | 1 + 2 files changed, 15 insertions(+) create mode 100644 source/implement_strstr/mod.rs diff --git a/source/implement_strstr/mod.rs b/source/implement_strstr/mod.rs new file mode 100644 index 0000000..e4f3fc9 --- /dev/null +++ b/source/implement_strstr/mod.rs @@ -0,0 +1,14 @@ +pub fn str_str(haystack: String, needle: String) -> i32 { + if needle.is_empty() { + return 0; + } + + haystack.find(&needle).map(|n| n as i32).unwrap_or(-1) +} + +#[test] +fn test_implement_strstr() { + assert_eq!(str_str("hello".to_string(), "ll".to_string()), 2); + assert_eq!(str_str("aaaaa".to_string(), "bba".to_string()), -1); + assert_eq!(str_str("aaaaa".to_string(), "".to_string()), 0); +} diff --git a/source/lib.rs b/source/lib.rs index 7708af6..1ec11ef 100644 --- a/source/lib.rs +++ b/source/lib.rs @@ -1,3 +1,4 @@ +pub mod implement_strstr; pub mod longest_common_prefix; pub mod palindrome_number; pub mod reverse_integer;