@@ -334,10 +334,11 @@ impl<K, V, S> HashMap<K, V, S> {
334
334
/// ```
335
335
/// use std::collections::HashMap;
336
336
///
337
- /// let mut map = HashMap::new();
338
- /// map.insert("a", 1);
339
- /// map.insert("b", 2);
340
- /// map.insert("c", 3);
337
+ /// let map = HashMap::from([
338
+ /// ("a", 1),
339
+ /// ("b", 2),
340
+ /// ("c", 3),
341
+ /// ]);
341
342
///
342
343
/// for key in map.keys() {
343
344
/// println!("{}", key);
@@ -356,10 +357,11 @@ impl<K, V, S> HashMap<K, V, S> {
356
357
/// ```
357
358
/// use std::collections::HashMap;
358
359
///
359
- /// let mut map = HashMap::new();
360
- /// map.insert("a", 1);
361
- /// map.insert("b", 2);
362
- /// map.insert("c", 3);
360
+ /// let map = HashMap::from([
361
+ /// ("a", 1),
362
+ /// ("b", 2),
363
+ /// ("c", 3),
364
+ /// ]);
363
365
///
364
366
/// for val in map.values() {
365
367
/// println!("{}", val);
@@ -378,11 +380,11 @@ impl<K, V, S> HashMap<K, V, S> {
378
380
/// ```
379
381
/// use std::collections::HashMap;
380
382
///
381
- /// let mut map = HashMap::new();
382
- ///
383
- /// map.insert("a ", 1);
384
- /// map.insert("b ", 2);
385
- /// map.insert("c", 3 );
383
+ /// let mut map = HashMap::from([
384
+ /// ("a", 1),
385
+ /// ("b ", 2),
386
+ /// ("c ", 3),
387
+ /// ] );
386
388
///
387
389
/// for val in map.values_mut() {
388
390
/// *val = *val + 10;
@@ -405,10 +407,11 @@ impl<K, V, S> HashMap<K, V, S> {
405
407
/// ```
406
408
/// use std::collections::HashMap;
407
409
///
408
- /// let mut map = HashMap::new();
409
- /// map.insert("a", 1);
410
- /// map.insert("b", 2);
411
- /// map.insert("c", 3);
410
+ /// let map = HashMap::from([
411
+ /// ("a", 1),
412
+ /// ("b", 2),
413
+ /// ("c", 3),
414
+ /// ]);
412
415
///
413
416
/// for (key, val) in map.iter() {
414
417
/// println!("key: {} val: {}", key, val);
@@ -428,10 +431,11 @@ impl<K, V, S> HashMap<K, V, S> {
428
431
/// ```
429
432
/// use std::collections::HashMap;
430
433
///
431
- /// let mut map = HashMap::new();
432
- /// map.insert("a", 1);
433
- /// map.insert("b", 2);
434
- /// map.insert("c", 3);
434
+ /// let mut map = HashMap::from([
435
+ /// ("a", 1),
436
+ /// ("b", 2),
437
+ /// ("c", 3),
438
+ /// ]);
435
439
///
436
440
/// // Update all values
437
441
/// for (_, val) in map.iter_mut() {
@@ -966,10 +970,11 @@ where
966
970
/// ```
967
971
/// use std::collections::HashMap;
968
972
///
969
- /// let mut map = HashMap::new();
970
- /// map.insert("a", 1);
971
- /// map.insert("b", 2);
972
- /// map.insert("c", 3);
973
+ /// let map = HashMap::from([
974
+ /// ("a", 1),
975
+ /// ("b", 2),
976
+ /// ("c", 3),
977
+ /// ]);
973
978
///
974
979
/// let mut vec: Vec<&str> = map.into_keys().collect();
975
980
/// // The `IntoKeys` iterator produces keys in arbitrary order, so the
@@ -992,10 +997,11 @@ where
992
997
/// ```
993
998
/// use std::collections::HashMap;
994
999
///
995
- /// let mut map = HashMap::new();
996
- /// map.insert("a", 1);
997
- /// map.insert("b", 2);
998
- /// map.insert("c", 3);
1000
+ /// let map = HashMap::from([
1001
+ /// ("a", 1),
1002
+ /// ("b", 2),
1003
+ /// ("c", 3),
1004
+ /// ]);
999
1005
///
1000
1006
/// let mut vec: Vec<i32> = map.into_values().collect();
1001
1007
/// // The `IntoValues` iterator produces values in arbitrary order, so
@@ -1202,8 +1208,9 @@ where
1202
1208
/// ```
1203
1209
/// use std::collections::HashMap;
1204
1210
///
1205
- /// let mut map = HashMap::new();
1206
- /// map.insert("a", 1);
1211
+ /// let map = HashMap::from([
1212
+ /// ("a", 1),
1213
+ /// ]);
1207
1214
/// let iter = map.iter();
1208
1215
/// ```
1209
1216
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1239,8 +1246,9 @@ impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
1239
1246
/// ```
1240
1247
/// use std::collections::HashMap;
1241
1248
///
1242
- /// let mut map = HashMap::new();
1243
- /// map.insert("a", 1);
1249
+ /// let mut map = HashMap::from([
1250
+ /// ("a", 1),
1251
+ /// ]);
1244
1252
/// let iter = map.iter_mut();
1245
1253
/// ```
1246
1254
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1269,8 +1277,9 @@ impl<'a, K, V> IterMut<'a, K, V> {
1269
1277
/// ```
1270
1278
/// use std::collections::HashMap;
1271
1279
///
1272
- /// let mut map = HashMap::new();
1273
- /// map.insert("a", 1);
1280
+ /// let map = HashMap::from([
1281
+ /// ("a", 1),
1282
+ /// ]);
1274
1283
/// let iter = map.into_iter();
1275
1284
/// ```
1276
1285
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1298,8 +1307,9 @@ impl<K, V> IntoIter<K, V> {
1298
1307
/// ```
1299
1308
/// use std::collections::HashMap;
1300
1309
///
1301
- /// let mut map = HashMap::new();
1302
- /// map.insert("a", 1);
1310
+ /// let map = HashMap::from([
1311
+ /// ("a", 1),
1312
+ /// ]);
1303
1313
/// let iter_keys = map.keys();
1304
1314
/// ```
1305
1315
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1335,8 +1345,9 @@ impl<K: Debug, V> fmt::Debug for Keys<'_, K, V> {
1335
1345
/// ```
1336
1346
/// use std::collections::HashMap;
1337
1347
///
1338
- /// let mut map = HashMap::new();
1339
- /// map.insert("a", 1);
1348
+ /// let map = HashMap::from([
1349
+ /// ("a", 1),
1350
+ /// ]);
1340
1351
/// let iter_values = map.values();
1341
1352
/// ```
1342
1353
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1372,8 +1383,9 @@ impl<K, V: Debug> fmt::Debug for Values<'_, K, V> {
1372
1383
/// ```
1373
1384
/// use std::collections::HashMap;
1374
1385
///
1375
- /// let mut map = HashMap::new();
1376
- /// map.insert("a", 1);
1386
+ /// let mut map = HashMap::from([
1387
+ /// ("a", 1),
1388
+ /// ]);
1377
1389
/// let iter = map.drain();
1378
1390
/// ```
1379
1391
#[ stable( feature = "drain" , since = "1.6.0" ) ]
@@ -1402,8 +1414,9 @@ impl<'a, K, V> Drain<'a, K, V> {
1402
1414
///
1403
1415
/// use std::collections::HashMap;
1404
1416
///
1405
- /// let mut map = HashMap::new();
1406
- /// map.insert("a", 1);
1417
+ /// let mut map = HashMap::from([
1418
+ /// ("a", 1),
1419
+ /// ]);
1407
1420
/// let iter = map.drain_filter(|_k, v| *v % 2 == 0);
1408
1421
/// ```
1409
1422
#[ unstable( feature = "hash_drain_filter" , issue = "59618" ) ]
@@ -1426,8 +1439,9 @@ where
1426
1439
/// ```
1427
1440
/// use std::collections::HashMap;
1428
1441
///
1429
- /// let mut map = HashMap::new();
1430
- /// map.insert("a", 1);
1442
+ /// let mut map = HashMap::from([
1443
+ /// ("a", 1),
1444
+ /// ]);
1431
1445
/// let iter_values = map.values_mut();
1432
1446
/// ```
1433
1447
#[ stable( feature = "map_values_mut" , since = "1.10.0" ) ]
@@ -1447,8 +1461,9 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> {
1447
1461
/// ```
1448
1462
/// use std::collections::HashMap;
1449
1463
///
1450
- /// let mut map = HashMap::new();
1451
- /// map.insert("a", 1);
1464
+ /// let map = HashMap::from([
1465
+ /// ("a", 1),
1466
+ /// ]);
1452
1467
/// let iter_keys = map.into_keys();
1453
1468
/// ```
1454
1469
#[ stable( feature = "map_into_keys_values" , since = "1.54.0" ) ]
@@ -1468,8 +1483,9 @@ pub struct IntoKeys<K, V> {
1468
1483
/// ```
1469
1484
/// use std::collections::HashMap;
1470
1485
///
1471
- /// let mut map = HashMap::new();
1472
- /// map.insert("a", 1);
1486
+ /// let map = HashMap::from([
1487
+ /// ("a", 1),
1488
+ /// ]);
1473
1489
/// let iter_keys = map.into_values();
1474
1490
/// ```
1475
1491
#[ stable( feature = "map_into_keys_values" , since = "1.54.0" ) ]
@@ -2004,10 +2020,11 @@ impl<K, V, S> IntoIterator for HashMap<K, V, S> {
2004
2020
/// ```
2005
2021
/// use std::collections::HashMap;
2006
2022
///
2007
- /// let mut map = HashMap::new();
2008
- /// map.insert("a", 1);
2009
- /// map.insert("b", 2);
2010
- /// map.insert("c", 3);
2023
+ /// let map = HashMap::from([
2024
+ /// ("a", 1),
2025
+ /// ("b", 2),
2026
+ /// ("c", 3),
2027
+ /// ]);
2011
2028
///
2012
2029
/// // Not possible with .iter()
2013
2030
/// let vec: Vec<(&str, i32)> = map.into_iter().collect();
0 commit comments