Skip to content

Commit df9b43e

Browse files
committed
Add an overloaded [] function to the map interface. Closes #2730.
1 parent 0c42a3f commit df9b43e

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/libstd/map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ iface map<K, V: copy> {
4545
"]
4646
fn get(K) -> V;
4747

48+
#[doc = "Like get, but as an operator."]
49+
fn [](K) -> V;
50+
4851
#[doc = "
4952
Get the value for the specified key. If the key does not exist in
5053
the map then returns none.
@@ -232,6 +235,10 @@ mod chained {
232235
option::get(self.find(k))
233236
}
234237

238+
fn [](k: K) -> V {
239+
option::get(self.find(k))
240+
}
241+
235242
fn remove(k: K) -> option<V> {
236243
alt self.search_tbl(k, self.hasher(k)) {
237244
not_found {none}

src/libstd/smallintmap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
7979
contains_key(self, key)
8080
}
8181
fn get(&&key: uint) -> V { get(self, key) }
82+
fn [](&&key: uint) -> V { get(self, key) }
8283
fn find(&&key: uint) -> option<V> { find(self, key) }
8384
fn rehash() { fail }
8485
fn each(it: fn(&&uint, V) -> bool) {

0 commit comments

Comments
 (0)