use ops::{Deref, FnMut, FnOnce, Index};
use option::Option::{self, Some, None};
use rand::{self, Rng};
-use result::Result;
use super::table::{
self,
/// }
/// ```
///
+/// `HashMap` also implements an [`Entry API`](#method.entry), which allows
+/// for more complex methods of getting, setting, updating and removing keys and
+/// their values:
+///
+/// ```
+/// use std::collections::HashMap;
+///
+/// // type inference lets us omit an explicit type signature (which
+/// // would be `HashMap<&str, u8>` in this example).
+/// let mut player_stats = HashMap::new();
+///
+/// fn random_stat_buff() -> u8 {
+/// // could actually return some random value here - let's just return
+/// // some fixed value for now
+/// 42
+/// }
+///
+/// // insert a key only if it doesn't already exist
+/// player_stats.entry("health").or_insert(100);
+///
+/// // insert a key using a function that provides a new value only if it
+/// // doesn't already exist
+/// player_stats.entry("defence").or_insert_with(random_stat_buff);
+///
+/// // update a key, guarding against the key possibly not being set
+/// let stat = player_stats.entry("attack").or_insert(100);
+/// *stat += random_stat_buff();
+/// ```
+///
/// The easiest way to use `HashMap` with a custom type as key is to derive `Eq` and `Hash`.
/// We must also derive `PartialEq`.
///
pub enum Entry<'a, K: 'a, V: 'a> {
/// An occupied Entry.
#[stable(feature = "rust1", since = "1.0.0")]
- Occupied(OccupiedEntry<'a, K, V>),
+ Occupied(
+ #[cfg_attr(not(stage0), stable(feature = "rust1", since = "1.0.0"))] OccupiedEntry<'a, K, V>
+ ),
/// A vacant Entry.
#[stable(feature = "rust1", since = "1.0.0")]
- Vacant(VacantEntry<'a, K, V>),
+ Vacant(
+ #[cfg_attr(not(stage0), stable(feature = "rust1", since = "1.0.0"))] VacantEntry<'a, K, V>
+ ),
}
/// Possible states of a VacantEntry.
use super::HashMap;
use super::Entry::{Occupied, Vacant};
- use iter::range_inclusive;
use cell::RefCell;
use rand::{thread_rng, Rng};
for _ in 0..10 {
assert!(m.is_empty());
- for i in range_inclusive(1, 1000) {
+ for i in 1..1001 {
assert!(m.insert(i, i).is_none());
- for j in range_inclusive(1, i) {
+ for j in 1..i+1 {
let r = m.get(&j);
assert_eq!(r, Some(&j));
}
- for j in range_inclusive(i+1, 1000) {
+ for j in i+1..1001 {
let r = m.get(&j);
assert_eq!(r, None);
}
}
- for i in range_inclusive(1001, 2000) {
+ for i in 1001..2001 {
assert!(!m.contains_key(&i));
}
// remove forwards
- for i in range_inclusive(1, 1000) {
+ for i in 1..1001 {
assert!(m.remove(&i).is_some());
- for j in range_inclusive(1, i) {
+ for j in 1..i+1 {
assert!(!m.contains_key(&j));
}
- for j in range_inclusive(i+1, 1000) {
+ for j in i+1..1001 {
assert!(m.contains_key(&j));
}
}
- for i in range_inclusive(1, 1000) {
+ for i in 1..1001 {
assert!(!m.contains_key(&i));
}
- for i in range_inclusive(1, 1000) {
+ for i in 1..1001 {
assert!(m.insert(i, i).is_none());
}
for i in (1..1001).rev() {
assert!(m.remove(&i).is_some());
- for j in range_inclusive(i, 1000) {
+ for j in i..1001 {
assert!(!m.contains_key(&j));
}
- for j in range_inclusive(1, i-1) {
+ for j in 1..i {
assert!(m.contains_key(&j));
}
}