fnfind_intersection(strings:&[&str])->String{ifstrings.is_empty(){// NOTE: mathematically speaking, the result here should be **all** the numbers - but we// obviously can't return that...return"".to_owned();// no arrays to intersect}letmutiterators:Vec<_>=strings.iter().map(|s|s.split(',').map(|n|n.trim().parse::<isize>().unwrap())).collect();letmutcurrent_values=Vec::with_capacity(iterators.len());foritiniterators.iter_mut(){ifletSome(value)=it.next(){current_values.push(value);}else{return"".to_owned();// at least one array is empty => intersection is empty}}letmutresult=Vec::<String>::new();loop{let(min_value,min_count)={letmutmin_value=current_values[0];letmutmin_count=1;forvalueincurrent_values.iter().skip(1){usestd::cmp::Ordering::*;matchvalue.cmp(&min_value){Less=>{min_value=*value;min_count=1;},Equal=>{min_count+=1;},Greater=>{},}};(min_value,min_count)};ifmin_count==current_values.len(){result.push(min_value.to_string());}for(current_value,it)incurrent_values.iter_mut().zip(iterators.iter_mut()){if*current_value==min_value{ifletSome(value)=it.next(){assert!(*current_value<=value,"numbers must be ordered");*current_value=value;}else{returnresult.join(", ");}}}}}
None that I'm aware of. I prefer "".to_owned() over String::from("") or "".to_string() to avoid the confusion of "why are you converting a string to a string?" that I've seen in some newbie Rust threads.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust solution - works for any number of strings:
Is there any difference between
and
?
None that I'm aware of. I prefer
"".to_owned()
overString::from("")
or"".to_string()
to avoid the confusion of "why are you converting a string to a string?" that I've seen in some newbie Rust threads.