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

String keys in HashMap are practically impossible #6095

Closed
Aatch opened this issue Apr 28, 2013 · 1 comment
Closed

String keys in HashMap are practically impossible #6095

Aatch opened this issue Apr 28, 2013 · 1 comment

Comments

@Aatch
Copy link
Contributor

Aatch commented Apr 28, 2013

type StringMap<T> = HashMap<~str, T>; //Fairly common

fn foo(map:&mut StringMap<uint>) {
  map.insert("Test".to_str(), 1); // This works fine, copying the string isn't unexpected
  map.find("Test"); //Nope. its a &'static str, not a ~str, so the types don't match, the only work around is copying the string
}

Copying the string in order to do a lookup is plain ridiculous. It adds significant overhead where it isn't needed. Now, I'm not sure how to fix it, but this is a common enough case that it should be dealt with.

Related to #3284 - probably.

@thestinger
Copy link
Contributor

You can use the find_equiv method to do a lookup with &str in a HashMap with ~str keys. This same issue exists in C++ with the standard containers, but there's no workaround like find_equiv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants