@@ -327,33 +327,196 @@ pub trait MutableSet<T>: Set<T> + Mutable {
327
327
328
328
/// A double-ended sequence that allows querying, insertion and deletion at both
329
329
/// ends.
330
+ ///
331
+ /// # Example
332
+ ///
333
+ /// With a `Deque` we can simulate a stack:
334
+ ///
335
+ /// ```
336
+ /// use std::collections::{RingBuf, Deque};
337
+ ///
338
+ /// let mut stack = RingBuf::new();
339
+ /// stack.push_front(1i);
340
+ /// stack.push_front(2i);
341
+ /// stack.push_front(3i);
342
+ ///
343
+ /// // Will print 3, 2, 1
344
+ /// while !stack.is_empty() {
345
+ /// let x = stack.pop_front().unwrap();
346
+ /// println!("{}", x);
347
+ /// }
348
+ /// ```
349
+ ///
350
+ /// We can simulate a queue:
351
+ ///
352
+ /// ```
353
+ /// use std::collections::{RingBuf, Deque};
354
+ ///
355
+ /// let mut queue = RingBuf::new();
356
+ /// queue.push_back(1i);
357
+ /// queue.push_back(2i);
358
+ /// queue.push_back(3i);
359
+ ///
360
+ /// // Will print 1, 2, 3
361
+ /// while !queue.is_empty() {
362
+ /// let x = queue.pop_front().unwrap();
363
+ /// println!("{}", x);
364
+ /// }
365
+ /// ```
366
+ ///
367
+ /// And of course we can mix and match:
368
+ ///
369
+ /// ```
370
+ /// use std::collections::{DList, Deque};
371
+ ///
372
+ /// let mut deque = DList::new();
373
+ ///
374
+ /// // Init deque with 1, 2, 3, 4
375
+ /// deque.push_front(2i);
376
+ /// deque.push_front(1i);
377
+ /// deque.push_back(3i);
378
+ /// deque.push_back(4i);
379
+ ///
380
+ /// // Will print (1, 4) and (2, 3)
381
+ /// while !deque.is_empty() {
382
+ /// let f = deque.pop_front().unwrap();
383
+ /// let b = deque.pop_back().unwrap();
384
+ /// println!("{}", (f, b));
385
+ /// }
386
+ /// ```
330
387
pub trait Deque < T > : Mutable {
331
- /// Provide a reference to the front element, or None if the sequence is
332
- /// empty
388
+ /// Provide a reference to the front element, or `None` if the sequence is.
389
+ /// empty.
390
+ ///
391
+ /// # Example
392
+ ///
393
+ /// ```
394
+ /// use std::collections::{RingBuf, Deque};
395
+ ///
396
+ /// let mut d = RingBuf::new();
397
+ /// assert_eq!(d.front(), None);
398
+ ///
399
+ /// d.push_back(1i);
400
+ /// d.push_back(2i);
401
+ /// assert_eq!(d.front(), Some(&1i));
402
+ /// ```
333
403
fn front < ' a > ( & ' a self ) -> Option < & ' a T > ;
334
404
335
- /// Provide a mutable reference to the front element, or None if the
336
- /// sequence is empty
405
+ /// Provide a mutable reference to the front element, or `None` if the
406
+ /// sequence is empty.
407
+ ///
408
+ /// # Example
409
+ ///
410
+ /// ```
411
+ /// use std::collections::{RingBuf, Deque};
412
+ ///
413
+ /// let mut d = RingBuf::new();
414
+ /// assert_eq!(d.front_mut(), None);
415
+ ///
416
+ /// d.push_back(1i);
417
+ /// d.push_back(2i);
418
+ /// match d.front_mut() {
419
+ /// Some(x) => *x = 9i,
420
+ /// None => (),
421
+ /// }
422
+ /// assert_eq!(d.front(), Some(&9i));
423
+ /// ```
337
424
fn front_mut < ' a > ( & ' a mut self ) -> Option < & ' a mut T > ;
338
425
339
426
/// Provide a reference to the back element, or None if the sequence is
340
- /// empty
427
+ /// empty.
428
+ ///
429
+ /// # Example
430
+ ///
431
+ /// ```
432
+ /// use std::collections::{DList, Deque};
433
+ ///
434
+ /// let mut d = DList::new();
435
+ /// assert_eq!(d.back(), None);
436
+ ///
437
+ /// d.push_back(1i);
438
+ /// d.push_back(2i);
439
+ /// assert_eq!(d.back(), Some(&2i));
440
+ /// ```
341
441
fn back < ' a > ( & ' a self ) -> Option < & ' a T > ;
342
442
343
443
/// Provide a mutable reference to the back element, or None if the sequence
344
- /// is empty
444
+ /// is empty.
445
+ ///
446
+ /// # Example
447
+ ///
448
+ /// ```
449
+ /// use std::collections::{DList, Deque};
450
+ ///
451
+ /// let mut d = DList::new();
452
+ /// assert_eq!(d.back(), None);
453
+ ///
454
+ /// d.push_back(1i);
455
+ /// d.push_back(2i);
456
+ /// match d.back_mut() {
457
+ /// Some(x) => *x = 9i,
458
+ /// None => (),
459
+ /// }
460
+ /// assert_eq!(d.back(), Some(&9i));
461
+ /// ```
345
462
fn back_mut < ' a > ( & ' a mut self ) -> Option < & ' a mut T > ;
346
463
347
- /// Insert an element first in the sequence
464
+ /// Insert an element first in the sequence.
465
+ ///
466
+ /// # Example
467
+ ///
468
+ /// ```
469
+ /// use std::collections::{DList, Deque};
470
+ ///
471
+ /// let mut d = DList::new();
472
+ /// d.push_front(1i);
473
+ /// d.push_front(2i);
474
+ /// assert_eq!(d.front(), Some(&2i));
348
475
fn push_front ( & mut self , elt : T ) ;
349
476
350
- /// Insert an element last in the sequence
477
+ /// Insert an element last in the sequence.
478
+ ///
479
+ /// # Example
480
+ ///
481
+ /// ```
482
+ /// use std::collections::{DList, Deque};
483
+ ///
484
+ /// let mut d = DList::new();
485
+ /// d.push_back(1i);
486
+ /// d.push_back(2i);
487
+ /// assert_eq!(d.front(), Some(&1i));
351
488
fn push_back ( & mut self , elt : T ) ;
352
489
353
- /// Remove the last element and return it, or None if the sequence is empty
490
+ /// Remove the last element and return it, or None if the sequence is empty.
491
+ ///
492
+ /// # Example
493
+ ///
494
+ /// ```
495
+ /// use std::collections::{RingBuf, Deque};
496
+ ///
497
+ /// let mut d = RingBuf::new();
498
+ /// d.push_back(1i);
499
+ /// d.push_back(2i);
500
+ ///
501
+ /// assert_eq!(d.pop_back(), Some(2i));
502
+ /// assert_eq!(d.pop_back(), Some(1i));
503
+ /// assert_eq!(d.pop_back(), None);
354
504
fn pop_back ( & mut self ) -> Option < T > ;
355
505
356
- /// Remove the first element and return it, or None if the sequence is empty
506
+ /// Remove the first element and return it, or None if the sequence is empty.
507
+ ///
508
+ /// # Example
509
+ ///
510
+ /// ```
511
+ /// use std::collections::{RingBuf, Deque};
512
+ ///
513
+ /// let mut d = RingBuf::new();
514
+ /// d.push_back(1i);
515
+ /// d.push_back(2i);
516
+ ///
517
+ /// assert_eq!(d.pop_front(), Some(1i));
518
+ /// assert_eq!(d.pop_front(), Some(2i));
519
+ /// assert_eq!(d.pop_front(), None);
357
520
fn pop_front ( & mut self ) -> Option < T > ;
358
521
}
359
522
0 commit comments