@@ -73,16 +73,46 @@ mod deque;
7373/// A trait to represent mutable containers
7474pub trait Mutable : Collection {
7575 /// Clear the container, removing all values.
76+ ///
77+ /// # Example
78+ ///
79+ /// ```
80+ /// let mut v = vec![1i, 2, 3];
81+ /// v.clear();
82+ /// assert!(v.is_empty());
83+ /// ```
7684 fn clear ( & mut self ) ;
7785}
7886
7987/// A map is a key-value store where values may be looked up by their keys. This
8088/// trait provides basic operations to operate on these stores.
8189pub trait Map < K , V > : Collection {
82- /// Return a reference to the value corresponding to the key
90+ /// Return a reference to the value corresponding to the key.
91+ ///
92+ /// # Example
93+ ///
94+ /// ```
95+ /// use std::collections::HashMap;
96+ ///
97+ /// let mut map = HashMap::new();
98+ /// map.insert("a", 1i);
99+ /// assert_eq!(map.find(&"a"), Some(&1i));
100+ /// assert_eq!(map.find(&"b"), None);
101+ /// ```
83102 fn find < ' a > ( & ' a self , key : & K ) -> Option < & ' a V > ;
84103
85- /// Return true if the map contains a value for the specified key
104+ /// Return true if the map contains a value for the specified key.
105+ ///
106+ /// # Example
107+ ///
108+ /// ```
109+ /// use std::collections::HashMap;
110+ ///
111+ /// let mut map = HashMap::new();
112+ /// map.insert("a", 1i);
113+ /// assert_eq!(map.contains_key(&"a"), true);
114+ /// assert_eq!(map.contains_key(&"b"), false);
115+ /// ```
86116 #[ inline]
87117 fn contains_key ( & self , key : & K ) -> bool {
88118 self . find ( key) . is_some ( )
@@ -94,45 +124,164 @@ pub trait MutableMap<K, V>: Map<K, V> + Mutable {
94124 /// Insert a key-value pair into the map. An existing value for a
95125 /// key is replaced by the new value. Return true if the key did
96126 /// not already exist in the map.
127+ ///
128+ /// # Example
129+ ///
130+ /// ```
131+ /// use std::collections::HashMap;
132+ ///
133+ /// let mut map = HashMap::new();
134+ /// assert_eq!(map.insert("key", 2i), true);
135+ /// assert_eq!(map.insert("key", 9i), false);
136+ /// assert_eq!(map.get(&"key"), &9i);
137+ /// ```
97138 #[ inline]
98139 fn insert ( & mut self , key : K , value : V ) -> bool {
99140 self . swap ( key, value) . is_none ( )
100141 }
101142
102143 /// Remove a key-value pair from the map. Return true if the key
103144 /// was present in the map, otherwise false.
145+ ///
146+ /// # Example
147+ ///
148+ /// ```
149+ /// use std::collections::HashMap;
150+ ///
151+ /// let mut map = HashMap::new();
152+ /// assert_eq!(map.remove(&"key"), false);
153+ /// map.insert("key", 2i);
154+ /// assert_eq!(map.remove(&"key"), true);
155+ /// ```
104156 #[ inline]
105157 fn remove ( & mut self , key : & K ) -> bool {
106158 self . pop ( key) . is_some ( )
107159 }
108160
109161 /// Insert a key-value pair from the map. If the key already had a value
110162 /// present in the map, that value is returned. Otherwise None is returned.
163+ ///
164+ /// # Example
165+ ///
166+ /// ```
167+ /// use std::collections::HashMap;
168+ ///
169+ /// let mut map = HashMap::new();
170+ /// assert_eq!(map.swap("a", 37i), None);
171+ /// assert_eq!(map.is_empty(), false);
172+ ///
173+ /// map.insert("a", 1i);
174+ /// assert_eq!(map.swap("a", 37i), Some(1i));
175+ /// assert_eq!(map.get(&"a"), &37i);
176+ /// ```
111177 fn swap ( & mut self , k : K , v : V ) -> Option < V > ;
112178
113179 /// Removes a key from the map, returning the value at the key if the key
114180 /// was previously in the map.
181+ ///
182+ /// # Example
183+ ///
184+ /// ```
185+ /// use std::collections::HashMap;
186+ ///
187+ /// let mut map: HashMap<&str, int> = HashMap::new();
188+ /// map.insert("a", 1i);
189+ /// assert_eq!(map.pop(&"a"), Some(1i));
190+ /// assert_eq!(map.pop(&"a"), None);
191+ /// ```
115192 fn pop ( & mut self , k : & K ) -> Option < V > ;
116193
117- /// Return a mutable reference to the value corresponding to the key
194+ /// Return a mutable reference to the value corresponding to the key.
195+ ///
196+ /// # Example
197+ ///
198+ /// ```
199+ /// use std::collections::HashMap;
200+ ///
201+ /// let mut map = HashMap::new();
202+ /// map.insert("a", 1i);
203+ /// match map.find_mut(&"a") {
204+ /// Some(x) => *x = 7i,
205+ /// None => (),
206+ /// }
207+ /// assert_eq!(map.get(&"a"), &7i);
208+ /// ```
118209 fn find_mut < ' a > ( & ' a mut self , key : & K ) -> Option < & ' a mut V > ;
119210}
120211
121212/// A set is a group of objects which are each distinct from one another. This
122213/// trait represents actions which can be performed on sets to iterate over
123214/// them.
124215pub trait Set < T > : Collection {
125- /// Return true if the set contains a value
216+ /// Return true if the set contains a value.
217+ ///
218+ /// # Example
219+ ///
220+ /// ```
221+ /// use std::collections::HashSet;
222+ ///
223+ /// let set: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
224+ /// assert_eq!(set.contains(&1), true);
225+ /// assert_eq!(set.contains(&4), false);
226+ /// ```
126227 fn contains ( & self , value : & T ) -> bool ;
127228
128229 /// Return true if the set has no elements in common with `other`.
129230 /// This is equivalent to checking for an empty intersection.
231+ ///
232+ /// # Example
233+ ///
234+ /// ```
235+ /// use std::collections::HashSet;
236+ ///
237+ /// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
238+ /// let mut b: HashSet<int> = HashSet::new();
239+ ///
240+ /// assert_eq!(a.is_disjoint(&b), true);
241+ /// b.insert(4);
242+ /// assert_eq!(a.is_disjoint(&b), true);
243+ /// b.insert(1);
244+ /// assert_eq!(a.is_disjoint(&b), false);
245+ /// ```
130246 fn is_disjoint ( & self , other : & Self ) -> bool ;
131247
132- /// Return true if the set is a subset of another
248+ /// Return true if the set is a subset of another.
249+ ///
250+ /// # Example
251+ ///
252+ /// ```
253+ /// use std::collections::HashSet;
254+ ///
255+ /// let sup: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
256+ /// let mut set: HashSet<int> = HashSet::new();
257+ ///
258+ /// assert_eq!(set.is_subset(&sup), true);
259+ /// set.insert(2);
260+ /// assert_eq!(set.is_subset(&sup), true);
261+ /// set.insert(4);
262+ /// assert_eq!(set.is_subset(&sup), false);
263+ /// ```
133264 fn is_subset ( & self , other : & Self ) -> bool ;
134265
135- /// Return true if the set is a superset of another
266+ /// Return true if the set is a superset of another.
267+ ///
268+ /// # Example
269+ ///
270+ /// ```
271+ /// use std::collections::HashSet;
272+ ///
273+ /// let sub: HashSet<int> = [1i, 2].iter().map(|&x| x).collect();
274+ /// let mut set: HashSet<int> = HashSet::new();
275+ ///
276+ /// assert_eq!(set.is_superset(&sub), false);
277+ ///
278+ /// set.insert(0);
279+ /// set.insert(1);
280+ /// assert_eq!(set.is_superset(&sub), false);
281+ ///
282+ /// set.insert(2);
283+ /// assert_eq!(set.is_superset(&sub), true);
284+ /// ```
136285 fn is_superset ( & self , other : & Self ) -> bool {
137286 other. is_subset ( self )
138287 }
@@ -145,10 +294,34 @@ pub trait Set<T>: Collection {
145294pub trait MutableSet < T > : Set < T > + Mutable {
146295 /// Add a value to the set. Return true if the value was not already
147296 /// present in the set.
297+ ///
298+ /// # Example
299+ ///
300+ /// ```
301+ /// use std::collections::HashSet;
302+ ///
303+ /// let mut set = HashSet::new();
304+ ///
305+ /// assert_eq!(set.insert(2i), true);
306+ /// assert_eq!(set.insert(2i), false);
307+ /// assert_eq!(set.len(), 1);
308+ /// ```
148309 fn insert ( & mut self , value : T ) -> bool ;
149310
150311 /// Remove a value from the set. Return true if the value was
151312 /// present in the set.
313+ ///
314+ /// # Example
315+ ///
316+ /// ```
317+ /// use std::collections::HashSet;
318+ ///
319+ /// let mut set = HashSet::new();
320+ ///
321+ /// set.insert(2i);
322+ /// assert_eq!(set.remove(&2), true);
323+ /// assert_eq!(set.remove(&2), false);
324+ /// ```
152325 fn remove ( & mut self , value : & T ) -> bool ;
153326}
154327
0 commit comments