From 74f1bfe76f552dd79608f3afcc9b37991accf8e6 Mon Sep 17 00:00:00 2001 From: stifskere Date: Thu, 17 Jul 2025 14:00:37 +0200 Subject: [PATCH 1/9] feat: hash_map macro --- library/alloc/src/macros.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index 1e6e2ae8c3675..14e1bcc024061 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -109,3 +109,18 @@ macro_rules! format { }) } } + +#[macro_export] +#[allow_internal_unstable(hint_must_use)] +#[rustc_diagnostic_item = "hash_map_macro"] +macro_rules! hash_map { + () => {{ + ::std::collections::HashMap::new() + }}; + + ( $( $key:expr => $value:expr ),* $(,)? ) => {{ + let mut __map = ::std::collections::HashMap::new(); + $( __map.insert($key, $value); )* + __map + }} +} From ff5f201d67155c15457dc265d2f64602c9aae6e5 Mon Sep 17 00:00:00 2001 From: stifskere Date: Thu, 17 Jul 2025 14:37:14 +0200 Subject: [PATCH 2/9] fix: hash_map macro location --- library/alloc/src/macros.rs | 15 --------------- library/std/src/macros.rs | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index 14e1bcc024061..1e6e2ae8c3675 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -109,18 +109,3 @@ macro_rules! format { }) } } - -#[macro_export] -#[allow_internal_unstable(hint_must_use)] -#[rustc_diagnostic_item = "hash_map_macro"] -macro_rules! hash_map { - () => {{ - ::std::collections::HashMap::new() - }}; - - ( $( $key:expr => $value:expr ),* $(,)? ) => {{ - let mut __map = ::std::collections::HashMap::new(); - $( __map.insert($key, $value); )* - __map - }} -} diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index 25e2b7ea13703..7055e77bb7d8d 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -379,3 +379,21 @@ macro_rules! dbg { ($($crate::dbg!($val)),+,) }; } + +/// Creates a [`HashMap`] +/// +/// [`HashMap`]: crate::collections::HashMap +#[macro_export] +#[allow_internal_unstable(hint_must_use)] +#[rustc_diagnostic_item = "hash_map_macro"] +macro_rules! hash_map { + () => {{ + ::std::collections::HashMap::new() + }}; + + ( $( $key:expr => $value:expr ),* $(,)? ) => {{ + let mut __map = ::std::collections::HashMap::new(); + $( __map.insert($key, $value); )* + __map + }} +} From b5692afa86a73dce5e34e267b2b58610029bdebe Mon Sep 17 00:00:00 2001 From: stifskere Date: Thu, 17 Jul 2025 14:48:38 +0200 Subject: [PATCH 3/9] docs: documented hash_map macro --- library/std/src/macros.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index 7055e77bb7d8d..67a872e0ca1d7 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -380,9 +380,37 @@ macro_rules! dbg { }; } -/// Creates a [`HashMap`] +/// Creates a [`HashMap`] containing the arguments. +/// +/// This macro creates or either an empty [`HashMap`] using +/// [`HashMap::new`] if there are no arguments, or generates +/// a [`HashMap::insert`] call for each element. +/// +/// This macro allows trailing commas. +/// +/// This macro is used by concatenating comma separated sequences +/// of ` => `. +/// +/// An example usage of this macro could be the following +/// +/// ```rust +/// let map = hash_map!{ +/// "key" => "value", +/// "key1" => "value1" +/// }; +/// +/// for (key, value) in map { +/// println!("{key:?} => {value:?}"); +/// } +/// ``` +/// +/// Note that since this macro only generates [`HashMap::insert`] +/// calls, the move semantics for the values are the same as +/// the insert method. /// /// [`HashMap`]: crate::collections::HashMap +/// [`HashMap::new`]: crate::collections::HashMap::new +/// [`HashMap::insert`]: crate::collections::HashMap::insert #[macro_export] #[allow_internal_unstable(hint_must_use)] #[rustc_diagnostic_item = "hash_map_macro"] From 4298c58af9151af26bfa7211c06670abd61081c0 Mon Sep 17 00:00:00 2001 From: stifskere Date: Thu, 17 Jul 2025 15:17:46 +0200 Subject: [PATCH 4/9] fix: hash_map macro annotations --- library/std/src/macros.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index 67a872e0ca1d7..799d2ea767bac 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -413,6 +413,7 @@ macro_rules! dbg { /// [`HashMap::insert`]: crate::collections::HashMap::insert #[macro_export] #[allow_internal_unstable(hint_must_use)] +#[unstable(feature = "hash_map_macro", issue = "144032")] #[rustc_diagnostic_item = "hash_map_macro"] macro_rules! hash_map { () => {{ From 7f00f3486d1ddb4ee1eff5930f3ab762590540f4 Mon Sep 17 00:00:00 2001 From: stifskere Date: Thu, 17 Jul 2025 16:04:18 +0200 Subject: [PATCH 5/9] fix: remove duplicate hash_map_macro feature --- library/std/src/macros.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index 799d2ea767bac..ffa24c4aa00ed 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -414,7 +414,6 @@ macro_rules! dbg { #[macro_export] #[allow_internal_unstable(hint_must_use)] #[unstable(feature = "hash_map_macro", issue = "144032")] -#[rustc_diagnostic_item = "hash_map_macro"] macro_rules! hash_map { () => {{ ::std::collections::HashMap::new() From 537c6093bed493bc236cdd01ba57fbb35919b0b1 Mon Sep 17 00:00:00 2001 From: stifskere Date: Thu, 17 Jul 2025 16:53:33 +0200 Subject: [PATCH 6/9] fix: add hash_map_macro feature in the crate --- library/std/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 311b2cb932392..7f14f7c3638b9 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -320,6 +320,7 @@ #![feature(try_trait_v2)] #![feature(type_alias_impl_trait)] #![feature(unsigned_signed_diff)] +#![feature(hash_map_macro)] // tidy-alphabetical-end // // Library features (core): From 3720f96d7b88f714f9745e586e23b5437c5e6daf Mon Sep 17 00:00:00 2001 From: stifskere Date: Thu, 17 Jul 2025 17:09:33 +0200 Subject: [PATCH 7/9] fix: change feature alphabetical order --- library/std/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 7f14f7c3638b9..477a110236100 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -294,6 +294,7 @@ #![feature(f128)] #![feature(ffi_const)] #![feature(formatting_options)] +#![feature(hash_map_macro)] #![feature(if_let_guard)] #![feature(intra_doc_pointers)] #![feature(iter_advance_by)] @@ -320,7 +321,6 @@ #![feature(try_trait_v2)] #![feature(type_alias_impl_trait)] #![feature(unsigned_signed_diff)] -#![feature(hash_map_macro)] // tidy-alphabetical-end // // Library features (core): From b57040d0b97387d0ceb78d833dc14d4bdec1ce4a Mon Sep 17 00:00:00 2001 From: stifskere Date: Thu, 17 Jul 2025 17:54:46 +0200 Subject: [PATCH 8/9] fix: add documentation feature flag for hash_map! --- library/std/src/macros.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index ffa24c4aa00ed..177a9208f5f4c 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -394,6 +394,8 @@ macro_rules! dbg { /// An example usage of this macro could be the following /// /// ```rust +/// #![feature(hash_map_macro)] +/// /// let map = hash_map!{ /// "key" => "value", /// "key1" => "value1" From 86cb6574f5635f30b1ff915381662beb32b29555 Mon Sep 17 00:00:00 2001 From: Esteve Autet Alexe Date: Fri, 18 Jul 2025 21:39:58 +0200 Subject: [PATCH 9/9] chore: change map name inside hash_map macro due to hygiene. --- library/std/src/macros.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/std/src/macros.rs b/library/std/src/macros.rs index 177a9208f5f4c..1a497ae32ba22 100644 --- a/library/std/src/macros.rs +++ b/library/std/src/macros.rs @@ -422,8 +422,8 @@ macro_rules! hash_map { }}; ( $( $key:expr => $value:expr ),* $(,)? ) => {{ - let mut __map = ::std::collections::HashMap::new(); - $( __map.insert($key, $value); )* - __map + let mut map = ::std::collections::HashMap::new(); + $( map.insert($key, $value); )* + map }} }