@@ -154,33 +154,196 @@ pub trait MutableSet<T>: Set<T> + Mutable {
154
154
155
155
/// A double-ended sequence that allows querying, insertion and deletion at both
156
156
/// ends.
157
+ ///
158
+ /// # Example
159
+ ///
160
+ /// With a `Deque` we can simulate a stack:
161
+ ///
162
+ /// ```
163
+ /// use std::collections::{RingBuf, Deque};
164
+ ///
165
+ /// let mut stack = RingBuf::new();
166
+ /// stack.push_front(1i);
167
+ /// stack.push_front(2i);
168
+ /// stack.push_front(3i);
169
+ ///
170
+ /// // Will print 3, 2, 1
171
+ /// while !stack.is_empty() {
172
+ /// let x = stack.pop_front().unwrap();
173
+ /// println!("{}", x);
174
+ /// }
175
+ /// ```
176
+ ///
177
+ /// We can simulate a queue:
178
+ ///
179
+ /// ```
180
+ /// use std::collections::{RingBuf, Deque};
181
+ ///
182
+ /// let mut queue = RingBuf::new();
183
+ /// queue.push_back(1i);
184
+ /// queue.push_back(2i);
185
+ /// queue.push_back(3i);
186
+ ///
187
+ /// // Will print 1, 2, 3
188
+ /// while !queue.is_empty() {
189
+ /// let x = queue.pop_front().unwrap();
190
+ /// println!("{}", x);
191
+ /// }
192
+ /// ```
193
+ ///
194
+ /// And of course we can mix and match:
195
+ ///
196
+ /// ```
197
+ /// use std::collections::{DList, Deque};
198
+ ///
199
+ /// let mut deque = DList::new();
200
+ ///
201
+ /// // Init deque with 1, 2, 3, 4
202
+ /// deque.push_front(2i);
203
+ /// deque.push_front(1i);
204
+ /// deque.push_back(3i);
205
+ /// deque.push_back(4i);
206
+ ///
207
+ /// // Will print (1, 4) and (2, 3)
208
+ /// while !deque.is_empty() {
209
+ /// let f = deque.pop_front().unwrap();
210
+ /// let b = deque.pop_back().unwrap();
211
+ /// println!("{}", (f, b));
212
+ /// }
213
+ /// ```
157
214
pub trait Deque < T > : Mutable {
158
- /// Provide a reference to the front element, or None if the sequence is
159
- /// empty
215
+ /// Provide a reference to the front element, or `None` if the sequence is.
216
+ /// empty.
217
+ ///
218
+ /// # Example
219
+ ///
220
+ /// ```
221
+ /// use std::collections::{RingBuf, Deque};
222
+ ///
223
+ /// let mut d = RingBuf::new();
224
+ /// assert_eq!(d.front(), None);
225
+ ///
226
+ /// d.push_back(1i);
227
+ /// d.push_back(2i);
228
+ /// assert_eq!(d.front(), Some(&1i));
229
+ /// ```
160
230
fn front < ' a > ( & ' a self ) -> Option < & ' a T > ;
161
231
162
- /// Provide a mutable reference to the front element, or None if the
163
- /// sequence is empty
232
+ /// Provide a mutable reference to the front element, or `None` if the
233
+ /// sequence is empty.
234
+ ///
235
+ /// # Example
236
+ ///
237
+ /// ```
238
+ /// use std::collections::{RingBuf, Deque};
239
+ ///
240
+ /// let mut d = RingBuf::new();
241
+ /// assert_eq!(d.front_mut(), None);
242
+ ///
243
+ /// d.push_back(1i);
244
+ /// d.push_back(2i);
245
+ /// match d.front_mut() {
246
+ /// Some(x) => *x = 9i,
247
+ /// None => (),
248
+ /// }
249
+ /// assert_eq!(d.front(), Some(&9i));
250
+ /// ```
164
251
fn front_mut < ' a > ( & ' a mut self ) -> Option < & ' a mut T > ;
165
252
166
253
/// Provide a reference to the back element, or None if the sequence is
167
- /// empty
254
+ /// empty.
255
+ ///
256
+ /// # Example
257
+ ///
258
+ /// ```
259
+ /// use std::collections::{DList, Deque};
260
+ ///
261
+ /// let mut d = DList::new();
262
+ /// assert_eq!(d.back(), None);
263
+ ///
264
+ /// d.push_back(1i);
265
+ /// d.push_back(2i);
266
+ /// assert_eq!(d.back(), Some(&2i));
267
+ /// ```
168
268
fn back < ' a > ( & ' a self ) -> Option < & ' a T > ;
169
269
170
270
/// Provide a mutable reference to the back element, or None if the sequence
171
- /// is empty
271
+ /// is empty.
272
+ ///
273
+ /// # Example
274
+ ///
275
+ /// ```
276
+ /// use std::collections::{DList, Deque};
277
+ ///
278
+ /// let mut d = DList::new();
279
+ /// assert_eq!(d.back(), None);
280
+ ///
281
+ /// d.push_back(1i);
282
+ /// d.push_back(2i);
283
+ /// match d.back_mut() {
284
+ /// Some(x) => *x = 9i,
285
+ /// None => (),
286
+ /// }
287
+ /// assert_eq!(d.back(), Some(&9i));
288
+ /// ```
172
289
fn back_mut < ' a > ( & ' a mut self ) -> Option < & ' a mut T > ;
173
290
174
- /// Insert an element first in the sequence
291
+ /// Insert an element first in the sequence.
292
+ ///
293
+ /// # Example
294
+ ///
295
+ /// ```
296
+ /// use std::collections::{DList, Deque};
297
+ ///
298
+ /// let mut d = DList::new();
299
+ /// d.push_front(1i);
300
+ /// d.push_front(2i);
301
+ /// assert_eq!(d.front(), Some(&2i));
175
302
fn push_front ( & mut self , elt : T ) ;
176
303
177
- /// Insert an element last in the sequence
304
+ /// Insert an element last in the sequence.
305
+ ///
306
+ /// # Example
307
+ ///
308
+ /// ```
309
+ /// use std::collections::{DList, Deque};
310
+ ///
311
+ /// let mut d = DList::new();
312
+ /// d.push_back(1i);
313
+ /// d.push_back(2i);
314
+ /// assert_eq!(d.front(), Some(&1i));
178
315
fn push_back ( & mut self , elt : T ) ;
179
316
180
- /// Remove the last element and return it, or None if the sequence is empty
317
+ /// Remove the last element and return it, or None if the sequence is empty.
318
+ ///
319
+ /// # Example
320
+ ///
321
+ /// ```
322
+ /// use std::collections::{RingBuf, Deque};
323
+ ///
324
+ /// let mut d = RingBuf::new();
325
+ /// d.push_back(1i);
326
+ /// d.push_back(2i);
327
+ ///
328
+ /// assert_eq!(d.pop_back(), Some(2i));
329
+ /// assert_eq!(d.pop_back(), Some(1i));
330
+ /// assert_eq!(d.pop_back(), None);
181
331
fn pop_back ( & mut self ) -> Option < T > ;
182
332
183
- /// Remove the first element and return it, or None if the sequence is empty
333
+ /// Remove the first element and return it, or None if the sequence is empty.
334
+ ///
335
+ /// # Example
336
+ ///
337
+ /// ```
338
+ /// use std::collections::{RingBuf, Deque};
339
+ ///
340
+ /// let mut d = RingBuf::new();
341
+ /// d.push_back(1i);
342
+ /// d.push_back(2i);
343
+ ///
344
+ /// assert_eq!(d.pop_front(), Some(1i));
345
+ /// assert_eq!(d.pop_front(), Some(2i));
346
+ /// assert_eq!(d.pop_front(), None);
184
347
fn pop_front ( & mut self ) -> Option < T > ;
185
348
}
186
349
0 commit comments