@@ -497,7 +497,13 @@ where
497497 /// ```
498498 #[ track_caller]
499499 pub fn insert_before ( & mut self , mut index : usize , key : K , value : V ) -> ( usize , Option < V > ) {
500- assert ! ( index <= self . len( ) , "index out of bounds" ) ;
500+ let len = self . len ( ) ;
501+
502+ assert ! (
503+ index <= len,
504+ "index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
505+ ) ;
506+
501507 match self . entry ( key) {
502508 Entry :: Occupied ( mut entry) => {
503509 if index > entry. index ( ) {
@@ -579,13 +585,21 @@ where
579585 let len = self . len ( ) ;
580586 match self . entry ( key) {
581587 Entry :: Occupied ( mut entry) => {
582- assert ! ( index < len, "index out of bounds" ) ;
588+ assert ! (
589+ index < len,
590+ "index out of bounds: the len is {len} but the index is {index}"
591+ ) ;
592+
583593 let old = mem:: replace ( entry. get_mut ( ) , value) ;
584594 entry. move_index ( index) ;
585595 Some ( old)
586596 }
587597 Entry :: Vacant ( entry) => {
588- assert ! ( index <= len, "index out of bounds" ) ;
598+ assert ! (
599+ index <= len,
600+ "index out of bounds: the len is {len} but the index is {index}. Expected index <= len"
601+ ) ;
602+
589603 entry. shift_insert ( index, value) ;
590604 None
591605 }
@@ -1332,7 +1346,7 @@ where
13321346 ///
13331347 /// ***Panics*** if `key` is not present in the map.
13341348 fn index ( & self , key : & Q ) -> & V {
1335- self . get ( key) . expect ( "IndexMap: key not found" )
1349+ self . get ( key) . expect ( "no entry found for key " )
13361350 }
13371351}
13381352
@@ -1374,7 +1388,7 @@ where
13741388 ///
13751389 /// ***Panics*** if `key` is not present in the map.
13761390 fn index_mut ( & mut self , key : & Q ) -> & mut V {
1377- self . get_mut ( key) . expect ( "IndexMap: key not found" )
1391+ self . get_mut ( key) . expect ( "no entry found for key " )
13781392 }
13791393}
13801394
@@ -1418,7 +1432,12 @@ impl<K, V, S> Index<usize> for IndexMap<K, V, S> {
14181432 /// ***Panics*** if `index` is out of bounds.
14191433 fn index ( & self , index : usize ) -> & V {
14201434 self . get_index ( index)
1421- . expect ( "IndexMap: index out of bounds" )
1435+ . unwrap_or_else ( || {
1436+ panic ! (
1437+ "index out of bounds: the len is {len} but the index is {index}" ,
1438+ len = self . len( )
1439+ ) ;
1440+ } )
14221441 . 1
14231442 }
14241443}
@@ -1457,8 +1476,12 @@ impl<K, V, S> IndexMut<usize> for IndexMap<K, V, S> {
14571476 ///
14581477 /// ***Panics*** if `index` is out of bounds.
14591478 fn index_mut ( & mut self , index : usize ) -> & mut V {
1479+ let len: usize = self . len ( ) ;
1480+
14601481 self . get_index_mut ( index)
1461- . expect ( "IndexMap: index out of bounds" )
1482+ . unwrap_or_else ( || {
1483+ panic ! ( "index out of bounds: the len is {len} but the index is {index}" ) ;
1484+ } )
14621485 . 1
14631486 }
14641487}
0 commit comments