-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Implement hash_map
macro
#144070
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
base: master
Are you sure you want to change the base?
Implement hash_map
macro
#144070
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -379,3 +379,77 @@ macro_rules! dbg { | |||||
($($crate::dbg!($val)),+,) | ||||||
}; | ||||||
} | ||||||
|
||||||
#[doc(hidden)] | ||||||
#[macro_export] | ||||||
#[allow_internal_unstable(hash_map_internals)] | ||||||
#[unstable(feature = "hash_map_internals", issue = "none")] | ||||||
macro_rules! repetition_utils { | ||||||
(@count $($tokens:tt),*) => {{ | ||||||
[$($crate::repetition_utils!(@replace $tokens => ())),*].len() | ||||||
}}; | ||||||
|
||||||
(@replace $x:tt => $y:tt) => { $y } | ||||||
} | ||||||
|
||||||
/// Creates a [`HashMap`] containing the arguments. | ||||||
/// | ||||||
/// `hash_map!` allows specifying the entries that make | ||||||
/// up the [`HashMap`] where the key and value are separated by a `=>`. | ||||||
/// | ||||||
/// The entries are separated by commas with a trailing comma being allowed. | ||||||
/// | ||||||
/// It is semantically equivalent to using repeated [`HashMap::insert`] | ||||||
/// on a newly created hashmap. | ||||||
/// | ||||||
/// `hash_map!` will attempt to avoid repeated reallocations by | ||||||
/// using [`HashMap::with_capacity`]. | ||||||
/// | ||||||
/// # Examples | ||||||
/// | ||||||
/// ```rust | ||||||
/// #![feature(hash_map_macro)] | ||||||
/// | ||||||
/// let map = hash_map! { | ||||||
/// "key" => "value", | ||||||
/// "key1" => "value1" | ||||||
/// }; | ||||||
/// | ||||||
/// assert_eq!(map.get("key"), Some(&"value")); | ||||||
/// assert_eq!(map.get("key1"), Some(&"value1")); | ||||||
/// assert!(map.get("brrrrrrooooommm").is_none()); | ||||||
/// ``` | ||||||
/// | ||||||
/// And with a trailing comma | ||||||
/// | ||||||
///```rust | ||||||
/// #![feature(hash_map_macro)] | ||||||
/// | ||||||
/// let map = hash_map! { | ||||||
/// "key" => "value", // notice the , | ||||||
/// }; | ||||||
/// | ||||||
/// assert_eq!(map.get("key"), Some(&"value")); | ||||||
/// ``` | ||||||
/// | ||||||
/// The key and value are moved into the HashMap. | ||||||
/// | ||||||
/// [`HashMap`]: crate::collections::HashMap | ||||||
/// [`HashMap::insert`]: crate::collections::HashMap::insert | ||||||
/// [`HashMap::with_capacity`]: crate::collections::HashMap::with_capacity | ||||||
#[macro_export] | ||||||
#[allow_internal_unstable(hash_map_internals)] | ||||||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
#[unstable(feature = "hash_map_macro", issue = "144032")] | ||||||
macro_rules! hash_map { | ||||||
() => {{ | ||||||
$crate::collections::HashMap::new() | ||||||
}}; | ||||||
|
||||||
( $( $key:expr => $value:expr ),* $(,)? ) => {{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very tiny nit 🙂:
Suggested change
would make the intent clearer, and also reject There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that makes sense, I suggest addressing this in a follow-up PR |
||||||
let mut map = $crate::collections::HashMap::with_capacity( | ||||||
const { $crate::repetition_utils!(@count $($key),*) } | ||||||
Noratrieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
); | ||||||
$( map.insert($key, $value); )* | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
map | ||||||
}} | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.