Skip to content

Commit c1ba63e

Browse files
committed
Merge pull request #21126 from sleepynate/dlist-examples
Dlist examples Reviewed-by: steveklabnik
2 parents 2aecd02 + fb55628 commit c1ba63e

File tree

1 file changed

+166
-6
lines changed

1 file changed

+166
-6
lines changed

src/libcollections/dlist.rs

+166-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -230,7 +230,7 @@ impl<T> DList<T> {
230230
///
231231
/// # Examples
232232
///
233-
/// ```rust
233+
/// ```
234234
/// use std::collections::DList;
235235
///
236236
/// let mut a = DList::new();
@@ -304,6 +304,18 @@ impl<T> DList<T> {
304304
/// Returns `true` if the `DList` is empty.
305305
///
306306
/// This operation should compute in O(1) time.
307+
///
308+
/// # Examples
309+
///
310+
/// ```
311+
/// use std::collections::DList;
312+
///
313+
/// let mut dl = DList::new();
314+
/// assert!(dl.is_empty());
315+
///
316+
/// dl.push_front("foo");
317+
/// assert!(!dl.is_empty());
318+
/// ```
307319
#[inline]
308320
#[stable]
309321
pub fn is_empty(&self) -> bool {
@@ -313,6 +325,24 @@ impl<T> DList<T> {
313325
/// Returns the length of the `DList`.
314326
///
315327
/// This operation should compute in O(1) time.
328+
///
329+
/// # Examples
330+
///
331+
/// ```
332+
/// use std::collections::DList;
333+
///
334+
/// let mut dl = DList::new();
335+
///
336+
/// dl.push_front(2is);
337+
/// assert_eq!(dl.len(), 1);
338+
///
339+
/// dl.push_front(1);
340+
/// assert_eq!(dl.len(), 2);
341+
///
342+
/// dl.push_back(3);
343+
/// assert_eq!(dl.len(), 3);
344+
///
345+
/// ```
316346
#[inline]
317347
#[stable]
318348
pub fn len(&self) -> uint {
@@ -322,6 +352,24 @@ impl<T> DList<T> {
322352
/// Removes all elements from the `DList`.
323353
///
324354
/// This operation should compute in O(n) time.
355+
///
356+
/// # Examples
357+
///
358+
/// ```
359+
/// use std::collections::DList;
360+
///
361+
/// let mut dl = DList::new();
362+
///
363+
/// dl.push_front(2is);
364+
/// dl.push_front(1);
365+
/// assert_eq!(dl.len(), 2);
366+
/// assert_eq!(dl.front(), Some(&1is));
367+
///
368+
/// dl.clear();
369+
/// assert_eq!(dl.len(), 0);
370+
/// assert_eq!(dl.front(), None);
371+
///
372+
/// ```
325373
#[inline]
326374
#[stable]
327375
pub fn clear(&mut self) {
@@ -330,6 +378,19 @@ impl<T> DList<T> {
330378

331379
/// Provides a reference to the front element, or `None` if the list is
332380
/// empty.
381+
///
382+
/// # Examples
383+
///
384+
/// ```
385+
/// use std::collections::DList;
386+
///
387+
/// let mut dl = DList::new();
388+
/// assert_eq!(dl.front(), None);
389+
///
390+
/// dl.push_front(1);
391+
/// assert_eq!(dl.front(), Some(&1is));
392+
///
393+
/// ```
333394
#[inline]
334395
#[stable]
335396
pub fn front(&self) -> Option<&T> {
@@ -338,6 +399,25 @@ impl<T> DList<T> {
338399

339400
/// Provides a mutable reference to the front element, or `None` if the list
340401
/// is empty.
402+
///
403+
/// # Examples
404+
///
405+
/// ```
406+
/// use std::collections::DList;
407+
///
408+
/// let mut dl = DList::new();
409+
/// assert_eq!(dl.front(), None);
410+
///
411+
/// dl.push_front(1);
412+
/// assert_eq!(dl.front(), Some(&1is));
413+
///
414+
/// match dl.front_mut() {
415+
/// None => {},
416+
/// Some(x) => *x = 5is,
417+
/// }
418+
/// assert_eq!(dl.front(), Some(&5is));
419+
///
420+
/// ```
341421
#[inline]
342422
#[stable]
343423
pub fn front_mut(&mut self) -> Option<&mut T> {
@@ -346,6 +426,19 @@ impl<T> DList<T> {
346426

347427
/// Provides a reference to the back element, or `None` if the list is
348428
/// empty.
429+
///
430+
/// # Examples
431+
///
432+
/// ```
433+
/// use std::collections::DList;
434+
///
435+
/// let mut dl = DList::new();
436+
/// assert_eq!(dl.back(), None);
437+
///
438+
/// dl.push_back(1);
439+
/// assert_eq!(dl.back(), Some(&1is));
440+
///
441+
/// ```
349442
#[inline]
350443
#[stable]
351444
pub fn back(&self) -> Option<&T> {
@@ -354,6 +447,25 @@ impl<T> DList<T> {
354447

355448
/// Provides a mutable reference to the back element, or `None` if the list
356449
/// is empty.
450+
///
451+
/// # Examples
452+
///
453+
/// ```
454+
/// use std::collections::DList;
455+
///
456+
/// let mut dl = DList::new();
457+
/// assert_eq!(dl.back(), None);
458+
///
459+
/// dl.push_back(1);
460+
/// assert_eq!(dl.back(), Some(&1is));
461+
///
462+
/// match dl.back_mut() {
463+
/// None => {},
464+
/// Some(x) => *x = 5is,
465+
/// }
466+
/// assert_eq!(dl.back(), Some(&5is));
467+
///
468+
/// ```
357469
#[inline]
358470
#[stable]
359471
pub fn back_mut(&mut self) -> Option<&mut T> {
@@ -363,6 +475,21 @@ impl<T> DList<T> {
363475
/// Adds an element first in the list.
364476
///
365477
/// This operation should compute in O(1) time.
478+
///
479+
/// # Examples
480+
///
481+
/// ```
482+
/// use std::collections::DList;
483+
///
484+
/// let mut dl = DList::new();
485+
///
486+
/// dl.push_front(2is);
487+
/// assert_eq!(dl.front().unwrap(), &2is);
488+
///
489+
/// dl.push_front(1);
490+
/// assert_eq!(dl.front().unwrap(), &1);
491+
///
492+
/// ```
366493
#[stable]
367494
pub fn push_front(&mut self, elt: T) {
368495
self.push_front_node(box Node::new(elt))
@@ -372,6 +499,23 @@ impl<T> DList<T> {
372499
/// empty.
373500
///
374501
/// This operation should compute in O(1) time.
502+
///
503+
/// # Examples
504+
///
505+
/// ```
506+
/// use std::collections::DList;
507+
///
508+
/// let mut d = DList::new();
509+
/// assert_eq!(d.pop_front(), None);
510+
///
511+
/// d.push_front(1is);
512+
/// d.push_front(3);
513+
/// assert_eq!(d.pop_front(), Some(3));
514+
/// assert_eq!(d.pop_front(), Some(1));
515+
/// assert_eq!(d.pop_front(), None);
516+
///
517+
/// ```
518+
///
375519
#[stable]
376520
pub fn pop_front(&mut self) -> Option<T> {
377521
self.pop_front_node().map(|box Node{value, ..}| value)
@@ -381,7 +525,7 @@ impl<T> DList<T> {
381525
///
382526
/// # Examples
383527
///
384-
/// ```rust
528+
/// ```
385529
/// use std::collections::DList;
386530
///
387531
/// let mut d = DList::new();
@@ -399,7 +543,7 @@ impl<T> DList<T> {
399543
///
400544
/// # Examples
401545
///
402-
/// ```rust
546+
/// ```
403547
/// use std::collections::DList;
404548
///
405549
/// let mut d = DList::new();
@@ -417,6 +561,22 @@ impl<T> DList<T> {
417561
/// including the index.
418562
///
419563
/// This operation should compute in O(n) time.
564+
/// # Examples
565+
///
566+
/// ```
567+
/// use std::collections::DList;
568+
///
569+
/// let mut d = DList::new();
570+
///
571+
/// d.push_front(1is);
572+
/// d.push_front(2);
573+
/// d.push_front(3);
574+
///
575+
/// let mut splitted = d.split_off(2);
576+
///
577+
/// assert_eq!(splitted.pop_front(), Some(1));
578+
/// assert_eq!(splitted.pop_front(), None);
579+
/// ```
420580
#[stable]
421581
pub fn split_off(&mut self, at: uint) -> DList<T> {
422582
let len = self.len();
@@ -593,7 +753,7 @@ impl<'a, A> IterMut<'a, A> {
593753
///
594754
/// # Examples
595755
///
596-
/// ```rust
756+
/// ```
597757
/// use std::collections::DList;
598758
///
599759
/// let mut list: DList<int> = vec![1, 3, 4].into_iter().collect();
@@ -619,7 +779,7 @@ impl<'a, A> IterMut<'a, A> {
619779
///
620780
/// # Examples
621781
///
622-
/// ```rust
782+
/// ```
623783
/// use std::collections::DList;
624784
///
625785
/// let mut list: DList<int> = vec![1, 2, 3].into_iter().collect();

0 commit comments

Comments
 (0)