Add an overloaded [] function to the map interface. Closes #2730.

This commit is contained in:
Michael Sullivan 2012-06-27 16:34:53 -07:00
parent 0c42a3ffee
commit df9b43e27c
2 changed files with 8 additions and 0 deletions

View File

@ -45,6 +45,9 @@
"]
fn get(K) -> V;
#[doc = "Like get, but as an operator."]
fn [](K) -> V;
#[doc = "
Get the value for the specified key. If the key does not exist in
the map then returns none.
@ -232,6 +235,10 @@ fn get(k: K) -> V {
option::get(self.find(k))
}
fn [](k: K) -> V {
option::get(self.find(k))
}
fn remove(k: K) -> option<V> {
alt self.search_tbl(k, self.hasher(k)) {
not_found {none}

View File

@ -79,6 +79,7 @@ fn contains_key(&&key: uint) -> bool {
contains_key(self, key)
}
fn get(&&key: uint) -> V { get(self, key) }
fn [](&&key: uint) -> V { get(self, key) }
fn find(&&key: uint) -> option<V> { find(self, key) }
fn rehash() { fail }
fn each(it: fn(&&uint, V) -> bool) {