diff --git a/src/collections.rs b/src/collections.rs index e69b077..4c88098 100644 --- a/src/collections.rs +++ b/src/collections.rs @@ -2,6 +2,30 @@ // Haystack of numbers, search for 47 // Haystack of strings, search for Agent. +fn search_needle(haystack: Vec, needle: T) -> Option { + haystack.iter().position(|&item| item == needle) +} + +fn main() { + // Haystack of numbers, search for 47 + let numbers: Vec = vec![10, 25, 47, 30, 15]; + match search_needle(numbers.clone(), 47) { + Some(index) => println!("Found 47 at index {}", index), + None => println!("47 not found in the numbers"), + } + + // Haystack of strings, search for "Agent" + let strings: Vec = vec![ + "Smith".to_string(), + "Agent".to_string(), + "Neo".to_string(), + ]; + match search_needle(strings.clone(), "Agent".to_string()) { + Some(index) => println!("Found 'Agent' at index {}", index), + None => println!("'Agent' not found in the strings"), + } +} + // Given a string of characters // Convert it to an array of characters // Print all the characters