mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-28 20:16:58 +03:00
std::hashmap: add an example with the basic methods.
This commit is contained in:
@@ -12,6 +12,45 @@
|
||||
//!
|
||||
//! The tables use a keyed hash with new random keys generated for each container, so the ordering
|
||||
//! of a set of keys in a hash table is randomized.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```rust
|
||||
//! use std::hashmap::HashMap;
|
||||
//!
|
||||
//! // type inference lets us omit an explicit type signature (which
|
||||
//! // would be `HashMap<&str, &str>` in this example).
|
||||
//! let mut book_reviews = HashMap::new();
|
||||
//!
|
||||
//! // review some books.
|
||||
//! book_reviews.insert("Adventures of Hucklebury Fin", "My favorite book.");
|
||||
//! book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
|
||||
//! book_reviews.insert("Pride and Prejudice", "Very enjoyable.");
|
||||
//! book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
|
||||
//!
|
||||
//! // check for a specific one.
|
||||
//! if !book_reviews.contains_key(& &"Les Misérables") {
|
||||
//! println!("We've got {} reviews, but Les Misérables ain't one.",
|
||||
//! book_reviews.len());
|
||||
//! }
|
||||
//!
|
||||
//! // oops, this review has a lot of spelling mistakes, let's delete it.
|
||||
//! book_reviews.remove(& &"The Adventures of Sherlock Holmes");
|
||||
//!
|
||||
//! // look up the values associated with some keys.
|
||||
//! let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
|
||||
//! for book in to_find.iter() {
|
||||
//! match book_reviews.find(book) {
|
||||
//! Some(review) => println!("{}: {}", *book, *review),
|
||||
//! None => println!("{} is unreviewed.", *book)
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! // iterate over everything.
|
||||
//! for (book, review) in book_reviews.iter() {
|
||||
//! println!("{}: \"{}\"", *book, *review);
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
|
||||
use clone::Clone;
|
||||
|
||||
Reference in New Issue
Block a user