Checking vector

v[0] = "0000" ;
v[1] = "1122" ;
v[2] = "3344" ;
v[3] = "5566" ;
v[4] = "7788" ;

var = "33" //  "4" "11" "99" "0" "00"

I need to check (on fly) does some of lines of v contain var in elegant (simple looks and fast works) way. (var itself is iteration)

// today's my algoritm
if v[1].contains(var)
 || v[2].contains(var)
 || v[3].contains(var)
 || v[4].contains(var)

? is there something like .filter()on vector to return boolean

? iterate over v

? convert v to a single str

I want to have ability to change v without recompiling source

You could use the .any(…) method on iterators to replace this if-condition by something like v.iter().any(|x| x.contains(var)).


Seems exactly what I need.
v.iter().any(|x| x.contains(var))
How to exclude v[0] ? Is there somthing indexOf(..) ? I do not want to create new v1 from v..

v.iter().skip(1).any(|x| x.contains(var))

This, or something like v[1..].iter().any(|x| x.contains(var)) would work, too, in this case.

As long as v1 is just a slice reference into v (instead of a whole new Vec copying data from v), there wouldn’t actually be any downside to this; i.e. let v1 = &v[1..]; if v1.iter().any(|x| x.contains(var)) { … } would be just as good as / equivalent to my code above.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.