@@ -1554,6 +1554,12 @@ impl<'a, K, V> Entry<'a, K, V> {
15541554}
15551555
15561556impl < ' a , K , V > OccupiedEntry < ' a , K , V > {
1557+ /// Gets a reference to the key in the entry.
1558+ #[ unstable( feature = "map_entry_keys" , issue = "32281" ) ]
1559+ pub fn key ( & self ) -> & K {
1560+ self . elem . read ( ) . 0
1561+ }
1562+
15571563 /// Gets a reference to the value in the entry.
15581564 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15591565 pub fn get ( & self ) -> & V {
@@ -1589,6 +1595,13 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
15891595}
15901596
15911597impl < ' a , K : ' a , V : ' a > VacantEntry < ' a , K , V > {
1598+ /// Gets a reference to the key that would be used when inserting a value
1599+ /// through the VacantEntry.
1600+ #[ unstable( feature = "map_entry_keys" , issue = "32281" ) ]
1601+ pub fn key ( & self ) -> & K {
1602+ & self . key
1603+ }
1604+
15921605 /// Sets the value of the entry with the VacantEntry's key,
15931606 /// and returns a mutable reference to it
15941607 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -2434,4 +2447,40 @@ mod test_map {
24342447 a. insert ( item, 0 ) ;
24352448 assert ! ( a. capacity( ) > a. len( ) ) ;
24362449 }
2450+
2451+ #[ test]
2452+ fn test_occupied_entry_key ( ) {
2453+ let mut a = HashMap :: new ( ) ;
2454+ let key = "hello there" ;
2455+ let value = "value goes here" ;
2456+ assert ! ( a. is_empty( ) ) ;
2457+ a. insert ( key. clone ( ) , value. clone ( ) ) ;
2458+ assert_eq ! ( a. len( ) , 1 ) ;
2459+ assert_eq ! ( a[ key] , value) ;
2460+
2461+ match a. entry ( key. clone ( ) ) {
2462+ Vacant ( _) => panic ! ( ) ,
2463+ Occupied ( e) => assert_eq ! ( key, * e. key( ) ) ,
2464+ }
2465+ assert_eq ! ( a. len( ) , 1 ) ;
2466+ assert_eq ! ( a[ key] , value) ;
2467+ }
2468+
2469+ #[ test]
2470+ fn test_vacant_entry_key ( ) {
2471+ let mut a = HashMap :: new ( ) ;
2472+ let key = "hello there" ;
2473+ let value = "value goes here" ;
2474+
2475+ assert ! ( a. is_empty( ) ) ;
2476+ match a. entry ( key. clone ( ) ) {
2477+ Occupied ( _) => panic ! ( ) ,
2478+ Vacant ( e) => {
2479+ assert_eq ! ( key, * e. key( ) ) ;
2480+ e. insert ( value. clone ( ) ) ;
2481+ } ,
2482+ }
2483+ assert_eq ! ( a. len( ) , 1 ) ;
2484+ assert_eq ! ( a[ key] , value) ;
2485+ }
24372486}
0 commit comments