I am currently calling !strval.matches(filter).collect::<Vec<_>>().is_empty()
to test whether a filter matches a given strval <&str>. It works.
It seems like it would be nice to have a variation that just return true if it finds a match, or false if it fails, thus stopping at the first match. Did I overlook an obvious method for doing this?
Thanks,
Joel
For any iterator, if you only care about whether it has at least one item, you can use .next().is_some().
That is clearly an improvement. Thanks. Given that iterators are lazy, will that also cause matches() to only be run up tot he first match? If so, it is exactly what I want..
Thanks,
Joel
You can also replace your expression with strval.find(filter).is_some(), which only looks for the first match (if any).
EDIT: Or just strval.contains(filter)
In general, you can assume that if a function returns an iterator rather than a collection, then it is doing so because a lazy implementation is possible, and it will only do as much work as required for each next(). Exceptions are marked; e.g. Itertools::sorted() specifies that it collects and sorts the input (as it must).
Which reminds me of a related question. As far as I can tell, Vec does not support a .contains method, even though if T supports PartialEq it seems like it just works. Did I miss the definition? Or is there some reason that is not defined?
Vec derefences to a slice, so you can use <[_]>::contains on any Vec.
Generally, Vec has methods that involve changing the length or capacity, or access to the raw pointer, and operations that read the contents are on the slice type.
Thanks. That is what I was looking for. I apparently missed how to find the documentation for what methods can be applied to slice. What search should I have entered into the std documentation to find it?
Yours,
Joel
slice - Rust or just search for "slice". Or if you have the Vec docs open anyway, all the slice methods are there under "Methods from Deref<Target=[T]>".
Thanks. I see it now.