No, it is not; however, you can easily filter before collecting, which in practice achieves the same effect.
If you want to filter by index, you need to add the index and then delete it later:
enumerate (to add an index to an element)filter based on this indexmap to remove an index from an element
Or in the code:
fn main() { let line = "Some line of text for example"; let l = line.split(" ") .enumerate() .filter(|&(i, _)| i == 3 ) .map(|(_, e)| e); let lvec: Vec<&str> = l.collect(); let text = &lvec[0]; println!("{}", text); }
If you want to get only one index (and therefore an element), then using nth much easier. It returns Option<&str> here, which you need to take care of:
fn main() { let line = "Some line of text for example"; let text = line.split(" ").nth(3).unwrap(); println!("{}", text); }
If you can have an arbitrary predicate, but only want the first element that matches, then collecting in Vec inefficient: it will consume the entire iterator (without laziness) and allocate potentially a lot of memory, which is not needed at all.
That way, you better just query the first element using the next iterator method, which returns Option<&str> here:
fn main() { let line = "Some line of text for example"; let text = line.split(" ") .enumerate() .filter(|&(i, _)| i % 7 == 3 ) .map(|(_, e)| e) .next() .unwrap(); println!("{}", text); }
If you want to select part of the result, by index, you can also use skip and take before building, but I think you already have the alternatives presented here.