Skip to content

Commit a168e30

Browse files
Rollup merge of rust-lang#34854 - GuillaumeGomez:linked_list_doc, r=steveklabnik
Add examples for LinkedList Part of rust-lang#29348. r? @steveklabnik
2 parents bcbe27c + 0c9a6f6 commit a168e30

File tree

1 file changed

+65
-9
lines changed

1 file changed

+65
-9
lines changed

src/libcollections/linked_list.rs

+65-9
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ impl<T> Default for LinkedList<T> {
172172

173173
impl<T> LinkedList<T> {
174174
/// Creates an empty `LinkedList`.
175+
///
176+
/// # Examples
177+
///
178+
/// ```
179+
/// use std::collections::LinkedList;
180+
///
181+
/// let list: LinkedList<u32> = LinkedList::new();
182+
/// ```
175183
#[inline]
176184
#[stable(feature = "rust1", since = "1.0.0")]
177185
pub fn new() -> Self {
@@ -226,6 +234,24 @@ impl<T> LinkedList<T> {
226234
}
227235

228236
/// Provides a forward iterator.
237+
///
238+
/// # Examples
239+
///
240+
/// ```
241+
/// use std::collections::LinkedList;
242+
///
243+
/// let mut list: LinkedList<u32> = LinkedList::new();
244+
///
245+
/// list.push_back(0);
246+
/// list.push_back(1);
247+
/// list.push_back(2);
248+
///
249+
/// let mut iter = list.iter();
250+
/// assert_eq!(iter.next(), Some(&0));
251+
/// assert_eq!(iter.next(), Some(&1));
252+
/// assert_eq!(iter.next(), Some(&2));
253+
/// assert_eq!(iter.next(), None);
254+
/// ```
229255
#[inline]
230256
#[stable(feature = "rust1", since = "1.0.0")]
231257
pub fn iter(&self) -> Iter<T> {
@@ -238,6 +264,28 @@ impl<T> LinkedList<T> {
238264
}
239265

240266
/// Provides a forward iterator with mutable references.
267+
///
268+
/// # Examples
269+
///
270+
/// ```
271+
/// use std::collections::LinkedList;
272+
///
273+
/// let mut list: LinkedList<u32> = LinkedList::new();
274+
///
275+
/// list.push_back(0);
276+
/// list.push_back(1);
277+
/// list.push_back(2);
278+
///
279+
/// for element in list.iter_mut() {
280+
/// *element += 10;
281+
/// }
282+
///
283+
/// let mut iter = list.iter();
284+
/// assert_eq!(iter.next(), Some(&10));
285+
/// assert_eq!(iter.next(), Some(&11));
286+
/// assert_eq!(iter.next(), Some(&12));
287+
/// assert_eq!(iter.next(), None);
288+
/// ```
241289
#[inline]
242290
#[stable(feature = "rust1", since = "1.0.0")]
243291
pub fn iter_mut(&mut self) -> IterMut<T> {
@@ -289,7 +337,6 @@ impl<T> LinkedList<T> {
289337
///
290338
/// dl.push_back(3);
291339
/// assert_eq!(dl.len(), 3);
292-
///
293340
/// ```
294341
#[inline]
295342
#[stable(feature = "rust1", since = "1.0.0")]
@@ -316,7 +363,6 @@ impl<T> LinkedList<T> {
316363
/// dl.clear();
317364
/// assert_eq!(dl.len(), 0);
318365
/// assert_eq!(dl.front(), None);
319-
///
320366
/// ```
321367
#[inline]
322368
#[stable(feature = "rust1", since = "1.0.0")]
@@ -326,6 +372,23 @@ impl<T> LinkedList<T> {
326372

327373
/// Returns `true` if the `LinkedList` contains an element equal to the
328374
/// given value.
375+
///
376+
/// # Examples
377+
///
378+
/// ```
379+
/// #![feature(linked_list_contains)]
380+
///
381+
/// use std::collections::LinkedList;
382+
///
383+
/// let mut list: LinkedList<u32> = LinkedList::new();
384+
///
385+
/// list.push_back(0);
386+
/// list.push_back(1);
387+
/// list.push_back(2);
388+
///
389+
/// assert_eq!(list.contains(&0), true);
390+
/// assert_eq!(list.contains(&10), false);
391+
/// ```
329392
#[unstable(feature = "linked_list_contains", reason = "recently added",
330393
issue = "32630")]
331394
pub fn contains(&self, x: &T) -> bool
@@ -347,7 +410,6 @@ impl<T> LinkedList<T> {
347410
///
348411
/// dl.push_front(1);
349412
/// assert_eq!(dl.front(), Some(&1));
350-
///
351413
/// ```
352414
#[inline]
353415
#[stable(feature = "rust1", since = "1.0.0")]
@@ -374,7 +436,6 @@ impl<T> LinkedList<T> {
374436
/// Some(x) => *x = 5,
375437
/// }
376438
/// assert_eq!(dl.front(), Some(&5));
377-
///
378439
/// ```
379440
#[inline]
380441
#[stable(feature = "rust1", since = "1.0.0")]
@@ -395,7 +456,6 @@ impl<T> LinkedList<T> {
395456
///
396457
/// dl.push_back(1);
397458
/// assert_eq!(dl.back(), Some(&1));
398-
///
399459
/// ```
400460
#[inline]
401461
#[stable(feature = "rust1", since = "1.0.0")]
@@ -422,7 +482,6 @@ impl<T> LinkedList<T> {
422482
/// Some(x) => *x = 5,
423483
/// }
424484
/// assert_eq!(dl.back(), Some(&5));
425-
///
426485
/// ```
427486
#[inline]
428487
#[stable(feature = "rust1", since = "1.0.0")]
@@ -446,7 +505,6 @@ impl<T> LinkedList<T> {
446505
///
447506
/// dl.push_front(1);
448507
/// assert_eq!(dl.front().unwrap(), &1);
449-
///
450508
/// ```
451509
#[stable(feature = "rust1", since = "1.0.0")]
452510
pub fn push_front(&mut self, elt: T) {
@@ -471,9 +529,7 @@ impl<T> LinkedList<T> {
471529
/// assert_eq!(d.pop_front(), Some(3));
472530
/// assert_eq!(d.pop_front(), Some(1));
473531
/// assert_eq!(d.pop_front(), None);
474-
///
475532
/// ```
476-
///
477533
#[stable(feature = "rust1", since = "1.0.0")]
478534
pub fn pop_front(&mut self) -> Option<T> {
479535
self.pop_front_node().map(Node::into_element)

0 commit comments

Comments
 (0)