Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for LinkedList #34854

Merged
merged 1 commit into from
Jul 21, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 65 additions & 9 deletions src/libcollections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ impl<T> Default for LinkedList<T> {

impl<T> LinkedList<T> {
/// Creates an empty `LinkedList`.
///
/// # Examples
///
/// ```
/// use std::collections::LinkedList;
///
/// let list: LinkedList<u32> = LinkedList::new();
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> Self {
Expand Down Expand Up @@ -226,6 +234,24 @@ impl<T> LinkedList<T> {
}

/// Provides a forward iterator.
///
/// # Examples
///
/// ```
/// use std::collections::LinkedList;
///
/// let mut list: LinkedList<u32> = LinkedList::new();
///
/// list.push_back(0);
/// list.push_back(1);
/// list.push_back(2);
///
/// let mut iter = list.iter();
/// assert_eq!(iter.next(), Some(&0));
/// assert_eq!(iter.next(), Some(&1));
/// assert_eq!(iter.next(), Some(&2));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Values needed to be updated here to account for += 10.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh damn, thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note comment still appears because it wasn't on the incorrect line. :p

/// assert_eq!(iter.next(), None);
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter(&self) -> Iter<T> {
Expand All @@ -238,6 +264,28 @@ impl<T> LinkedList<T> {
}

/// Provides a forward iterator with mutable references.
///
/// # Examples
///
/// ```
/// use std::collections::LinkedList;
///
/// let mut list: LinkedList<u32> = LinkedList::new();
///
/// list.push_back(0);
/// list.push_back(1);
/// list.push_back(2);
///
/// for element in list.iter_mut() {
/// *element += 10;
/// }
///
/// let mut iter = list.iter();
/// assert_eq!(iter.next(), Some(&10));
/// assert_eq!(iter.next(), Some(&11));
/// assert_eq!(iter.next(), Some(&12));
/// assert_eq!(iter.next(), None);
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn iter_mut(&mut self) -> IterMut<T> {
Expand Down Expand Up @@ -289,7 +337,6 @@ impl<T> LinkedList<T> {
///
/// dl.push_back(3);
/// assert_eq!(dl.len(), 3);
///
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -316,7 +363,6 @@ impl<T> LinkedList<T> {
/// dl.clear();
/// assert_eq!(dl.len(), 0);
/// assert_eq!(dl.front(), None);
///
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -326,6 +372,23 @@ impl<T> LinkedList<T> {

/// Returns `true` if the `LinkedList` contains an element equal to the
/// given value.
///
/// # Examples
///
/// ```
/// #![feature(linked_list_contains)]
///
/// use std::collections::LinkedList;
///
/// let mut list: LinkedList<u32> = LinkedList::new();
///
/// list.push_back(0);
/// list.push_back(1);
/// list.push_back(2);
///
/// assert_eq!(list.contains(&0), true);
/// assert_eq!(list.contains(&10), false);
/// ```
#[unstable(feature = "linked_list_contains", reason = "recently added",
issue = "32630")]
pub fn contains(&self, x: &T) -> bool
Expand All @@ -347,7 +410,6 @@ impl<T> LinkedList<T> {
///
/// dl.push_front(1);
/// assert_eq!(dl.front(), Some(&1));
///
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -374,7 +436,6 @@ impl<T> LinkedList<T> {
/// Some(x) => *x = 5,
/// }
/// assert_eq!(dl.front(), Some(&5));
///
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -395,7 +456,6 @@ impl<T> LinkedList<T> {
///
/// dl.push_back(1);
/// assert_eq!(dl.back(), Some(&1));
///
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -422,7 +482,6 @@ impl<T> LinkedList<T> {
/// Some(x) => *x = 5,
/// }
/// assert_eq!(dl.back(), Some(&5));
///
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -446,7 +505,6 @@ impl<T> LinkedList<T> {
///
/// dl.push_front(1);
/// assert_eq!(dl.front().unwrap(), &1);
///
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push_front(&mut self, elt: T) {
Expand All @@ -471,9 +529,7 @@ impl<T> LinkedList<T> {
/// assert_eq!(d.pop_front(), Some(3));
/// assert_eq!(d.pop_front(), Some(1));
/// assert_eq!(d.pop_front(), None);
///
/// ```
///
#[stable(feature = "rust1", since = "1.0.0")]
pub fn pop_front(&mut self) -> Option<T> {
self.pop_front_node().map(Node::into_element)
Expand Down