Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hashmap! + hashset! macros #48937

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2709,6 +2709,39 @@ fn assert_covariance() {
}
}

/// Creates a `HashMap` containing the provided key-value pairs.
///
/// `hashmap!` allows `HashMap`s to be defined with the same syntax as an array of tuples.
///
/// ```
/// use std::collections::HashMap;
///
/// let m = hashmap![("one", 1), ("two", 2), ("three", 3)];
/// assert_eq!(m["one"], 1);
/// assert_eq!(m["two"], 2);
/// assert_eq!(m["three"], 3);
///
/// let mut m = hashmap![];
/// m.insert("one", 1);
/// m.insert("two", 2);
/// m.insert("three", 3);
///
/// assert_eq!(m, hashmap![("one", 1), ("three", 3), ("two", 2),]);
/// ```
#[macro_export]
macro_rules! hashmap {
($(($key:expr, $value:expr)),*) => (
{
let mut map = HashMap::new();
$(
map.insert($key, $value);
)*
map
}
);
($(($key:expr, $value:expr),)*) => (hashmap![$(($key, $value)),*])
}

#[cfg(test)]
mod test_map {
use super::HashMap;
Expand Down Expand Up @@ -3651,4 +3684,24 @@ mod test_map {
let _ = panic::catch_unwind(panic::AssertUnwindSafe(|| { hm.entry(0) <- makepanic(); }));
assert_eq!(hm.len(), 0);
}

#[test]
fn test_macro() {
let mut expected = HashMap::new();
expected.insert("this is a test", 'a');
expected.insert("это испытание", 'b');
expected.insert("هذا اختبار", 'c');

assert_eq!(expected, hashmap![("this is a test", 'a'), ("هذا اختبار", 'c'), ("это испытание", 'b'),]);

let mut second_a = HashMap::new();
let mut second_b = hashmap![];

second_a.insert("three", 3.0);
second_a.insert("four", 4.0);
second_b.insert("three", 3.0);
second_b.insert("four", 4.0);

assert_eq!(second_a, second_b);
}
}
53 changes: 53 additions & 0 deletions src/libstd/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,39 @@ fn assert_covariance() {
}
}

/// Creates a `HashSet` containing the provided key-value pairs.
///
/// `hashset!` allows `HashSet`s to be defined with the same syntax as array expressions.
///
/// ```
/// use std::collections::HashSet;
///
/// let s = hashset!["one", "two", "three"];
/// assert!(s.contains("one"));
/// assert!(s.contains("two"));
/// assert!(s.contains("three"));
///
/// let mut s = hashset![];
/// s.insert("one");
/// s.insert("two");
/// s.insert("three");
///
/// assert_eq!(s, hashset!["one", "three", "two",]);
/// ```
#[macro_export]
macro_rules! hashset {
($($key:expr),*) => (
{
let mut set = HashSet::new();
$(
set.insert($key);
)*
set
}
);
($($key:expr,)*) => (hashset![$($key),*])
}

#[cfg(test)]
mod test_set {
use super::HashSet;
Expand Down Expand Up @@ -1752,4 +1785,24 @@ mod test_set {
assert!(set.contains(&4));
assert!(set.contains(&6));
}

#[test]
fn test_macro() {
let mut expected = HashSet::new();
expected.insert("this is a test");
expected.insert("это испытание");
expected.insert("هذا اختبار");

assert_eq!(expected, hashset!["this is a test", "هذا اختبار", "это испытание",]);

let mut second_a = HashSet::new();
let mut second_b = hashset![];

second_a.insert("one");
second_a.insert("two");
second_b.insert("one");
second_b.insert("two");

assert_eq!(second_a, second_b);
}
}