Crate to_vec [−] [src]
to_vec
, to_set andto_map
are specializations of collect
in the
usual case where you do want these containers.
use to_vec::ToVec; let v = "one two three".split_whitespace().to_vec(); assert_eq!(v,&["one","two","three"]);
There's a specialized form for collecting Result<T,E>
into
Result<Vec<T>,E>
, where the error is the first error encountered.
use to_vec::ToVecResult; let numbers = "23E 5F5 FF00".split_whitespace() .map(|s| u32::from_str_radix(s,16)).to_vec_result().unwrap(); assert_eq!(numbers,&[0x23E, 0x5F5, 0xFF00]);
to_map
and to_set
are different - they operate on iterators
of references and implicitly clone this.
use to_vec::ToMap; const VALUES: &[(&str,i32)] = &[("hello",10),("dolly",20)]; let map = VALUES.iter().to_map(); assert_eq!(map.get("hello"),Some(&10)); assert_eq!(map.get("dolly"),Some(&20));
This implicit cloning behaviour is very useful for sets (here defined
as HashSet
):
use to_vec::ToSet; let colours = ["green","orange","blue"].iter().to_set(); let fruit = ["apple","banana","orange"].iter().to_set(); let common = colours.intersection(&fruit).to_set(); assert_eq!(common, ["orange"].iter().to_set());
Traits
ToMap |
to_map() method on iterators of references |
ToSet |
to_set() method on iterators of references |
ToVec |
to_vec() method on iterators |
ToVecResult |
to_vec_result() method on iterators |