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;