[−][src]Macro velcro::map_iter
map_iter!() { /* proc-macro */ }
Creates an iterator over pairs of values, expressed with map-like syntax.
Other collections and iterators may also be interspersed, or "spread", using the
..
operator.
Usage
use velcro::map_iter; let vec = vec![0, 1, 2, 3]; for (key, value) in map_iter![0: "a", 1: "b", ..(2..=5): "c"] { println!("{} = {}", key, value); } assert_eq!( map_iter! { 0: "a", 1: "b", ..(2..=5): "c" }.collect::<Vec<_>>(), vec![(0, "a"), (1, "b"), (2, "c"), (3, "c"), (4, "c"), (5, "c")] );
A typical use-case for map_iter
is to collect into a third party map
implementation, not supported by velcro, while still being able to use the
velcro spread operator.
For example, IndexMap
from the indexmap
crate:
use velcro::map_iter; use indexmap::map::IndexMap; let map: IndexMap<_, _> = map_iter! { ..(0..10): 100, 1: 200, ..(3..5): 300 } .collect(); assert_eq!(map.get(&0).unwrap(), &100); assert_eq!(map.get(&1).unwrap(), &200); assert_eq!(map.get(&2).unwrap(), &100); assert_eq!(map.get(&3).unwrap(), &300);