Macro mapcomp::hashmapc [−][src]
macro_rules! hashmapc { (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; if $cond:expr) => { ... }; (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr) => { ... }; (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; if $cond:expr; $($tail:tt)+) => { ... }; (@__ $acc:ident, $key:expr => $val:expr; for $item:pat in $iter:expr; $($tail:tt)+) => { ... }; ($key:expr => $val:expr; $($tail:tt)+) => { ... }; }
Hash Map Comprehension
Creates a HashMap
from the contents of the comprehension. It is
analogous to Python's dictionary comprehension except that it uses the =>
token instead of a colon.
Python code:
numbers = [6, 4, 18]
halves = {str(x): x / 2 for x in numbers}
Rust equivalent code:
let numbers = [6, 4, 18]; let halves = hashmapc!{x.to_string() => x / 2; for x in numbers.iter()}; for &(k, v) in &[("6", 3), ("4", 2), ("18", 9)] { assert_eq!(halves[k], v); }