1
Fork 0
leetcode/source/implement_strstr/mod.rs

15 lines
396 B
Rust

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);
}