@@ -73,16 +73,46 @@ mod deque;
73
73
/// A trait to represent mutable containers
74
74
pub trait Mutable : Collection {
75
75
/// 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
+ /// ```
76
84
fn clear ( & mut self ) ;
77
85
}
78
86
79
87
/// A map is a key-value store where values may be looked up by their keys. This
80
88
/// trait provides basic operations to operate on these stores.
81
89
pub 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
+ /// ```
83
102
fn find < ' a > ( & ' a self , key : & K ) -> Option < & ' a V > ;
84
103
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
+ /// ```
86
116
#[ inline]
87
117
fn contains_key ( & self , key : & K ) -> bool {
88
118
self . find ( key) . is_some ( )
@@ -94,45 +124,164 @@ pub trait MutableMap<K, V>: Map<K, V> + Mutable {
94
124
/// Insert a key-value pair into the map. An existing value for a
95
125
/// key is replaced by the new value. Return true if the key did
96
126
/// 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
+ /// ```
97
138
#[ inline]
98
139
fn insert ( & mut self , key : K , value : V ) -> bool {
99
140
self . swap ( key, value) . is_none ( )
100
141
}
101
142
102
143
/// Remove a key-value pair from the map. Return true if the key
103
144
/// 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
+ /// ```
104
156
#[ inline]
105
157
fn remove ( & mut self , key : & K ) -> bool {
106
158
self . pop ( key) . is_some ( )
107
159
}
108
160
109
161
/// Insert a key-value pair from the map. If the key already had a value
110
162
/// 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
+ /// ```
111
177
fn swap ( & mut self , k : K , v : V ) -> Option < V > ;
112
178
113
179
/// Removes a key from the map, returning the value at the key if the key
114
180
/// 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
+ /// ```
115
192
fn pop ( & mut self , k : & K ) -> Option < V > ;
116
193
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
+ /// ```
118
209
fn find_mut < ' a > ( & ' a mut self , key : & K ) -> Option < & ' a mut V > ;
119
210
}
120
211
121
212
/// A set is a group of objects which are each distinct from one another. This
122
213
/// trait represents actions which can be performed on sets to iterate over
123
214
/// them.
124
215
pub 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
+ /// ```
126
227
fn contains ( & self , value : & T ) -> bool ;
127
228
128
229
/// Return true if the set has no elements in common with `other`.
129
230
/// 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
+ /// ```
130
246
fn is_disjoint ( & self , other : & Self ) -> bool ;
131
247
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
+ /// ```
133
264
fn is_subset ( & self , other : & Self ) -> bool ;
134
265
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
+ /// ```
136
285
fn is_superset ( & self , other : & Self ) -> bool {
137
286
other. is_subset ( self )
138
287
}
@@ -145,10 +294,34 @@ pub trait Set<T>: Collection {
145
294
pub trait MutableSet < T > : Set < T > + Mutable {
146
295
/// Add a value to the set. Return true if the value was not already
147
296
/// 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
+ /// ```
148
309
fn insert ( & mut self , value : T ) -> bool ;
149
310
150
311
/// Remove a value from the set. Return true if the value was
151
312
/// 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
+ /// ```
152
325
fn remove ( & mut self , value : & T ) -> bool ;
153
326
}
154
327
0 commit comments