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

Conversation

hotandcoldfever
Copy link

These macros let you make HashMaps and HashSets filled w/ items in 1 pass much like vec!. They can also be used as a slightly shorter alternative to HashMap::new()/HashSet::new() for empty HashMaps/HashSets

HashMap!'s syntax's meant to be similar to using the insert method:

let mut book_reviews = HashMap::new();
book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book.");
book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
let mut book_reviews = hashmap![("Adventures of Huckleberry Finn", "My favorite book."), ("Grimms' Fairy Tales", "Masterpiece.")];

No feature gates coz apparently feature gates don't work for macros
Issues: these macros're available even when HashMap/HashSet ain't in scope, tho I ain't sure how much of a problem that is in practice since I imagine people'll type use std::collections::HashMap 1st

fn main() {
    let map = hashmap![("test", 1)];
}
error[E0433]: failed to resolve. Use of undeclared type or module `HashMap`
 --> src/main.rs:2:15
  |
2 |     let map = hashmap![("test", 1)];
  |               ^^^^^^^^^^^^^^^^^^^^^ Use of undeclared type or module `HashMap`
  |

Maybe add HashMap and HashSet to the prelude but I don't think that's likely

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @shepmaster (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 11, 2018
@hotandcoldfever
Copy link
Author

Maybe this should be in an RFC?

@udoprog
Copy link
Contributor

udoprog commented Mar 11, 2018

Hey @cool-cool-sweat, this looks really neat!

This feature is already present in an active RFC. You might want to look through it to see if it meets your needs, and possibly make your voice heard there: rust-lang/rfcs#542

@kennytm kennytm added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 11, 2018
@hotandcoldfever
Copy link
Author

I'm gonna close this in favour of the pull request I've made against maplit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants