You now have a good understanding of iterators and closures and how they can be used to process collections of data.
In this challenge, you will use iterators to filter out duplicate items from a collection of potentially redundant entries.
Define a function that takes a collection of items (as an iterator) and returns a collection of unique items in sorted order. The items are represented as strings. You are expected to handle duplicates and ignore entries that are empty or consist solely of whitespace.
Your function signature and return types must be determined by you, but the input must implement the Iterator
trait, ensuring flexibility for different iterator sources.
String
, &String
, or &str
items.fn unique_items<I, T>(items: I) -> Vec<String>
where
I: Iterator<Item = T>,
T: AsRef<str>,
{
// Your code here
}
HashSet
to track unique items.filter_map
method to remove invalid entries (e.g., empty or whitespace-only strings).trim
method on strings to handle whitespace effectively.HashSet
provides a method inesert
that returns a bool
indicating whether the item was already present.Vec
and call the sort
method.use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<I, T>(item: I) -> Vec<String>where I : Iterator<Item = T >, T : AsRef<str> { let set: HashSet<String> = item.filter_map(|element|{ let element_str = element.as_ref().trim(); if !element_str.is_empty() {Some(element_str.to_string())} else {None} }) .collect(); let mut new_vec: Vec<String> = set.into_iter().collect(); new_vec.sort(); new_vec }// Requirements// Filter duplicates: Ensure that the resulting collection contains only unique items.// Ignore invalid entries: Exclude entries that are empty or only consist of whitespace.// Sort the output: Return the items sorted in ascending lexicographical order.// Flexibility: The function should work with any iterator that has String, &String, or &str items./// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<I, T>(item: I) -> Vec<String>where I : Iterator<Item = T >, T : AsRef<str> { let set: HashSet<String> = item.filter_map(|element|{ let element_str = element.as_ref().trim(); if !element_str.is_empty() {Some(element_str.to_string())} else {None} }) .collect(); let mut new_vec: Vec<String> = set.into_iter().collect(); new_vec.sort(); new_vec }/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
// 1. Finish the function// pub fn unique_items ...use std::collections::HashSet;pub fn unique_items<I,T>(iter :I) -> Vec<String> where I: Iterator<Item=T>, T: AsRef<str> { let mut result: Vec<_> = iter.map(|item| item.as_ref().trim().to_string()) .filter(|item| !item.is_empty()) .collect::<HashSet<String>>() .into_iter() .collect(); result.sort(); result}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<T : Eq + std::hash::Hash + AsRef<str>>(iter : impl Iterator<Item = T>) -> Vec<String> { let mut seen = iter .map(|item| item.as_ref().trim().to_string()) .filter(|item| !item.is_empty()) .collect::<HashSet<String>>() .into_iter() .collect::<Vec<String>>(); seen.sort(); seen}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<I, T>(items: I) -> Vec<String> where I: Iterator<Item = T>, T: AsRef<str>,{ let mut unique_items = items .map(|x| x.as_ref().trim().to_string()) .filter(|x| !x.is_empty()) .collect::<HashSet<String>>() .into_iter() .collect::<Vec<String>>(); unique_items.sort(); unique_items} /// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the function// pub fn unique_items ...pub fn unique_items<I, R>(item: I) -> Vec<String> where I: Iterator<Item=R>, R: AsRef<str> { let mut normalized: Vec<String> = item.into_iter() .map(|item| item.as_ref().trim().to_string()) .collect::<HashSet<String>>().into_iter().collect(); normalized.retain(|item| !item.is_empty()); normalized.sort(); normalized }/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<I, T>(iter: I) -> Vec<String> where I: Iterator<Item = T>, T: AsRef<str>,{ let mut set = HashSet::<String>::new(); for item in iter { let trimmed_item = &item.as_ref().trim(); if *trimmed_item != "" { set.insert(trimmed_item.to_string()); } } let mut vec = set.into_iter().collect::<Vec<String>>(); vec.sort(); vec}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;use std::hash::Hash;// 1. Finish the function// pub fn unique_items ...pub fn unique_items<I, T>(items: I) -> Vec<String>where I: Iterator<Item = T>, T: AsRef<str>,{ let mut unique_items: Vec<String> = items.map(|x| x.as_ref().trim().to_string()) .filter(|x| !x.is_empty()) .collect::<HashSet<String>>() .into_iter() .collect::<Vec<String>>(); unique_items.sort(); unique_items}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the function// pub fn unique_items ...pub fn unique_items<I, T>(list: I) -> Vec<String>where I: Iterator<Item=T>, T: AsRef<str>{ let mut normalized: Vec<String> = list.into_iter() .map(|item| item.as_ref().trim().to_string()) .collect::<HashSet<String>>().into_iter().collect(); normalized.retain(|item| !item.is_empty()); normalized.sort(); normalized}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the function// pub fn unique_items ...pub fn unique_items<I, T>(items: I) -> Vec<String>where I: Iterator<Item = T>, T: AsRef<str>,{ let mut set: HashSet<String> = HashSet::new(); for item in items { let s = item.as_ref().trim().to_string(); if s.is_empty() || s.chars().all(|c| c == ' ') { continue; } if !set.contains(&s) { set.insert(s); } } // Convert set to vec let mut v: Vec<String> = set.drain().collect(); v.sort(); v}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<T>(iter: impl Iterator<Item = T>) -> Vec<String>where T : AsRef<str>{ let preprocessed: Vec<String> = iter.map(|s| s.as_ref().trim().to_string()).filter(|s|!s.is_empty()).collect(); let mut set: HashSet<String> = HashSet::new(); for e in preprocessed { set.insert(e); } let mut res: Vec<String> = set.into_iter().collect(); res.sort(); // println!("{:?}", res); res}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let x = product_ids.into_iter(); let unique_ids = unique_items(x); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<T>(iter: impl Iterator<Item = T>) -> Vec<String>where T : AsRef<str>{ let preprocessed: Vec<String> = iter.map(|s| s.as_ref().trim().to_string()).filter(|s|!s.is_empty()).collect(); let mut set: HashSet<String> = HashSet::new(); for e in preprocessed { set.insert(e); } let mut res: Vec<String> = set.into_iter().collect(); res.sort(); // println!("{:?}", res); res}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let x = product_ids.into_iter(); let unique_ids = unique_items(x); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<T>(iter: impl Iterator<Item = T>) -> Vec<String>where T : AsRef<str> + std::cmp::Eq + std::hash::Hash + Clone + Ord{ let preprocessed: Vec<String> = iter.map(|s| s.as_ref().trim().to_string()).filter(|s|!s.is_empty()).collect(); let mut set: HashSet<String> = HashSet::new(); for e in preprocessed { set.insert(e); } let mut res: Vec<String> = set.into_iter().collect(); res.sort(); // println!("{:?}", res); res}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let x = product_ids.into_iter(); let unique_ids = unique_items(x); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::{collections::HashSet, ops::Deref};// 1. Finish the function// pub fn unique_items ...pub fn unique_items(iter: impl Iterator<Item = impl AsRef<str>>) -> Vec<String> { let mut found: HashSet<String> = HashSet::new(); let mut result = iter .map(|s| s.as_ref().trim().to_string()) .filter(|s| { if s.is_empty() { return false; } if found.contains(s) { return false; } found.insert(s.to_string()); true }) .collect::<Vec<String>>(); result.sort(); result}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
// 1. Finish the functionpub fn unique_items<I, T>(items: I) -> Vec<String> where I: Iterator<Item=T>, T: AsRef<str>,{ let mut unique: Vec<String> = vec![]; items .map(|i| i.as_ref().trim().to_string()) .filter(|i| !i.is_empty()) .for_each(|i| { if !unique.contains(&i) { unique.push(i); } }); unique.sort(); unique} /// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<I, T>(items: I) -> Vec<String>where I: Iterator<Item = T>, T: AsRef<str>{ let mut set: HashSet<String> = HashSet::new(); items .filter(|s| !s.as_ref().trim().is_empty()) .for_each(|s| { let s2 = s.as_ref().trim().to_string(); let _ = set.insert(s2); }); let mut v = Vec::from_iter(set.into_iter()); v.sort(); v}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;use std::hash::RandomState;// 1. Finish the function// pub fn unique_items ...pub fn unique_items<I, T>(items: I) -> Vec<String>where I: Iterator<Item = T>, T: AsRef<str>,{ let temp: HashSet<String>=HashSet::from_iter(items.map(|x| x.as_ref().trim().to_string()).filter(|x| !x.is_empty())); let mut ret=Vec::from_iter(temp.iter().map(|x| x.clone())); ret.sort(); return ret;}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
// 1. Finish the functionpub fn unique_items<I, T>(iter: I) -> Vec<String>where I: Iterator<Item = T>, T: AsRef<str>,{ let mut unique_ids = Vec::new(); iter.map(|s| s.as_ref().trim().to_string()) .filter(|id| !id.is_empty()) .for_each(|id| { if !unique_ids.contains(&id) { unique_ids.push(id); } }); unique_ids.sort(); unique_ids}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the function// pub fn unique_items ...pub fn unique_items<I, T>(items: I) -> Vec<String>where I: Iterator<Item=T>, T: AsRef<str>,{ let mut unique = items.filter_map(|item| { let i = item.as_ref().trim(); if i.is_empty() { return None; } else { Some(i.to_string()) } }).collect::<HashSet<String>>().into_iter().collect::<Vec<String>>(); unique.sort(); unique}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
// 1. Finish the functionpub fn unique_items<I, T>(iter: I) -> Vec<String>where I: Iterator<Item = T>, T: AsRef<str> + Ord,{ let mut vec = iter .map(|item| item.as_ref().trim().to_string()) .filter(|item| !item.is_empty()) .collect::<Vec<_>>(); vec.sort(); vec.dedup(); vec}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<T>(iter: impl Iterator<Item = T>) -> Vec<String> where T: AsRef<str> { let mut strings: HashSet<String> = HashSet::new(); let mut result = iter .filter_map(|x| { let s = x.as_ref().trim().to_string(); if s.is_empty() { None } else { if strings.insert(s.clone()) { Some(s) } else { None } } }) .collect::<Vec<String>>(); result.sort(); return result;}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;pub fn unique_items<T>(iter: impl Iterator<Item = T>) -> Vec<String>where T: AsRef<str>,{ let mut set: HashSet<String> = HashSet::new(); let mut result = iter .filter_map(|x| { let s = x.as_ref().trim().to_string(); if s.is_empty() { None } else { if set.insert(s.clone()) { Some(s) } else { None } } }) .collect::<Vec<String>>(); result.sort(); result}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;use std::hash::Hash;pub fn unique_items<T>(iter: impl Iterator<Item = T>) -> Vec<String>where T: Hash + Ord + AsRef<str> { let mut result = iter .filter_map(|s| { let s = s.as_ref().trim(); if !s.is_empty() { Some(s.to_string()) } else { None } }) .collect::<HashSet<_>>() .into_iter() .collect::<Vec<String>>(); result.sort(); result}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;pub fn unique_items<I, T>(items: I) -> Vec<String> where I: Iterator<Item = T>, T: AsRef<str>,{ let mut unique_set: HashSet<String> = HashSet::new(); let mut result = items .filter_map(|item| { let trimmed_item = item.as_ref().trim().to_string(); if trimmed_item.is_empty() { None } else { if unique_set.insert(trimmed_item.clone()) { Some(trimmed_item) } else { None } } }) .collect::<Vec<String>>(); result.sort(); result}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<I, T>(items: I) -> Vec<String> where I: Iterator<Item = T>, T: AsRef<str> + ToString,{ let mut unique: HashSet<String> = HashSet::new(); let mut result = items .filter_map(|i| if !i.as_ref().trim().is_empty() { // if not only whitespace let item = i.as_ref().trim().to_string(); // trim out any whitespace if unique.insert(item) { Some(i.as_ref().trim().to_string()) } else { None } } else { None } ) .map(|x| x.trim().to_string()) .collect::<Vec<String>>(); result.sort(); result}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::HashSet;// 1. Finish the functionpub fn unique_items<I, T>(items: I) -> Vec<String> where I: Iterator<Item = T>, T: AsRef<str> + ToString,{ let mut unique: HashSet<String> = HashSet::new(); let mut result = items .filter_map(|i| if !i.as_ref().trim().is_empty() { // if not only whitespace let item = i.as_ref().trim().to_string(); // trim out any whitespace if unique.insert(item) { Some(i.as_ref().trim().to_string()) } else { None } } else { None } ) .map(|x| x.trim().to_string()) .collect::<Vec<String>>(); result.sort(); result}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::BTreeSet;// 1. Finish the function// pub fn unique_items ...pub fn unique_items(iter: impl Iterator<Item = impl AsRef<str> + Ord>) -> Vec<String> { iter.filter_map(|it| { if it.as_ref().is_empty() || it.as_ref().chars().all(|c| c.is_whitespace()) { None } else { Some(it.as_ref().trim().to_string()) } }) .collect::<BTreeSet<_>>() .into_iter() .collect()}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::BTreeSet;// 1. Finish the function// pub fn unique_items ...pub fn unique_items(iter: impl Iterator<Item = impl AsRef<str> + Ord>) -> Vec<String> { iter.filter_map(|it| { if it.as_ref().is_empty() || it.as_ref().chars().all(|c| c.is_whitespace()) { None } else { Some(it.as_ref().trim().to_string()) } }) .collect::<BTreeSet<_>>() .into_iter() .collect()}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::{collections::HashSet, hash::Hash};// 1. Finish the function// pub fn unique_items ...pub fn unique_items<I, T>(items: I) -> Vec<String> where I: Iterator<Item = T>, T: AsRef<str> + Eq + Hash + Ord,{ let mut hs: HashSet<String> = HashSet::new(); // let non_empty = items // .filter(|i| !i.as_ref().trim().is_empty()) // .collect(); for i in items { let t = i.as_ref().trim(); if !t.is_empty() { let _ = hs.insert(t.to_string()); } } let mut v = Vec::from_iter(hs); v.sort(); v}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}
use std::collections::{HashSet};pub fn unique_items<I, K>(iterator: I) -> Vec<String>where K: AsRef<str>, I: Iterator<Item = K>,{ let mut seen = HashSet::new(); let mut result: Vec<String> = iterator .filter_map(|item| { let trimmed = item.as_ref().trim().to_string(); if !trimmed.is_empty() && seen.insert(trimmed.clone()) { Some(trimmed) } else { None } }) .collect(); result.sort(); result}/// Example usagepub fn main() { let product_ids = vec![ "abc123".to_string(), " ".to_string(), "def456".to_string(), "abc123".to_string(), "ghi789".to_string(), "ghi789".to_string(), " def456".to_string(), ]; let unique_ids = unique_items(product_ids.into_iter()); assert_eq!(unique_ids, vec!["abc123", "def456", "ghi789"]);}