Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
// Haystack of numbers, search for 47
// Haystack of strings, search for Agent.

fn search_needle<T: PartialEq>(haystack: Vec<T>, needle: T) -> Option<usize> {
haystack.iter().position(|&item| item == needle)
}

fn main() {
// Haystack of numbers, search for 47
let numbers: Vec<i32> = 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<String> = 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
Expand Down