@@ -264,27 +264,35 @@ fn test_resize_policy() {
264
264
/// }
265
265
/// ```
266
266
///
267
- /// The easiest way to use `HashMap` with a custom type is to derive `Eq` and `Hash`.
267
+ /// The easiest way to use `HashMap` with a custom type as key is to derive `Eq` and `Hash`.
268
268
/// We must also derive `PartialEq`.
269
269
///
270
270
/// ```
271
271
/// use std::collections::HashMap;
272
272
///
273
273
/// #[deriving(Hash, Eq, PartialEq, Show)]
274
- /// struct Viking<'a> {
275
- /// name: &'a str ,
276
- /// power: uint ,
274
+ /// struct Viking {
275
+ /// name: String ,
276
+ /// country: String ,
277
277
/// }
278
278
///
279
+ /// impl Viking {
280
+ /// /// Create a new Viking.
281
+ /// pub fn new(name: &str, country: &str) -> Viking {
282
+ /// Viking { name: name.to_string(), country: country.to_string() }
283
+ /// }
284
+ /// }
285
+ ///
286
+ /// // Use a HashMap to store the vikings' health points.
279
287
/// let mut vikings = HashMap::new();
280
288
///
281
- /// vikings.insert("Norway ", Viking { name: "Einar", power: 9u } );
282
- /// vikings.insert("Denmark ", Viking { name: "Olaf", power: 4u } );
283
- /// vikings.insert("Iceland ", Viking { name: "Harald", power: 8u } );
289
+ /// vikings.insert(Viking::new("Einar ", "Norway"), 25u );
290
+ /// vikings.insert(Viking::new("Olaf ", "Denmark"), 24u );
291
+ /// vikings.insert(Viking::new("Harald ", "Iceland"), 12u );
284
292
///
285
- /// // Use derived implementation to print the vikings.
286
- /// for (land, viking ) in vikings.iter() {
287
- /// println!("{} at {}", viking, land );
293
+ /// // Use derived implementation to print the status of the vikings.
294
+ /// for (viking, health ) in vikings.iter() {
295
+ /// println!("{} has {} hp ", viking, health );
288
296
/// }
289
297
/// ```
290
298
#[ deriving( Clone ) ]
0 commit comments