Skip to content

Commit fb4c3f0

Browse files
committed
auto merge of #15752 : nham/rust/dlist_docs, r=alexcrichton
Someone rightfully complained in IRC that DList was lacking examples. Here are some.
2 parents 44a71de + 7ee45aa commit fb4c3f0

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/libcollections/dlist.rs

+91
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@ impl<T> DList<T> {
278278
/// Move the last element to the front of the list.
279279
///
280280
/// If the list is empty, do nothing.
281+
///
282+
/// # Example
283+
///
284+
/// ```rust
285+
/// use std::collections::{DList, Deque};
286+
///
287+
/// let mut dl = DList::new();
288+
/// dl.push_back(1i);
289+
/// dl.push_back(2);
290+
/// dl.push_back(3);
291+
///
292+
/// dl.rotate_forward();
293+
///
294+
/// for e in dl.iter() {
295+
/// println!("{}", e); // prints 3, then 1, then 2
296+
/// }
297+
/// ```
281298
#[inline]
282299
pub fn rotate_forward(&mut self) {
283300
self.pop_back_node().map(|tail| {
@@ -288,6 +305,23 @@ impl<T> DList<T> {
288305
/// Move the first element to the back of the list.
289306
///
290307
/// If the list is empty, do nothing.
308+
///
309+
/// # Example
310+
///
311+
/// ```rust
312+
/// use std::collections::{DList, Deque};
313+
///
314+
/// let mut dl = DList::new();
315+
/// dl.push_back(1i);
316+
/// dl.push_back(2);
317+
/// dl.push_back(3);
318+
///
319+
/// dl.rotate_backward();
320+
///
321+
/// for e in dl.iter() {
322+
/// println!("{}", e); // prints 2, then 3, then 1
323+
/// }
324+
/// ```
291325
#[inline]
292326
pub fn rotate_backward(&mut self) {
293327
self.pop_front_node().map(|head| {
@@ -298,6 +332,25 @@ impl<T> DList<T> {
298332
/// Add all elements from `other` to the end of the list
299333
///
300334
/// O(1)
335+
///
336+
/// # Example
337+
///
338+
/// ```rust
339+
/// use std::collections::{DList, Deque};
340+
///
341+
/// let mut a = DList::new();
342+
/// let mut b = DList::new();
343+
/// a.push_back(1i);
344+
/// a.push_back(2);
345+
/// b.push_back(3i);
346+
/// b.push_back(4);
347+
///
348+
/// a.append(b);
349+
///
350+
/// for e in a.iter() {
351+
/// println!("{}", e); // prints 1, then 2, then 3, then 4
352+
/// }
353+
/// ```
301354
pub fn append(&mut self, mut other: DList<T>) {
302355
match self.list_tail.resolve() {
303356
None => *self = other,
@@ -320,6 +373,25 @@ impl<T> DList<T> {
320373
/// Add all elements from `other` to the beginning of the list
321374
///
322375
/// O(1)
376+
///
377+
/// # Example
378+
///
379+
/// ```rust
380+
/// use std::collections::{DList, Deque};
381+
///
382+
/// let mut a = DList::new();
383+
/// let mut b = DList::new();
384+
/// a.push_back(1i);
385+
/// a.push_back(2);
386+
/// b.push_back(3i);
387+
/// b.push_back(4);
388+
///
389+
/// a.prepend(b);
390+
///
391+
/// for e in a.iter() {
392+
/// println!("{}", e); // prints 3, then 4, then 1, then 2
393+
/// }
394+
/// ```
323395
#[inline]
324396
pub fn prepend(&mut self, mut other: DList<T>) {
325397
mem::swap(self, &mut other);
@@ -330,6 +402,25 @@ impl<T> DList<T> {
330402
/// or at the end.
331403
///
332404
/// O(N)
405+
///
406+
/// # Example
407+
///
408+
/// ```rust
409+
/// use std::collections::{DList, Deque};
410+
///
411+
/// let mut a: DList<int> = DList::new();
412+
/// a.push_back(2i);
413+
/// a.push_back(4);
414+
/// a.push_back(7);
415+
/// a.push_back(8);
416+
///
417+
/// // insert 11 before the first odd number in the list
418+
/// a.insert_when(11, |&e, _| e % 2 == 1);
419+
///
420+
/// for e in a.iter() {
421+
/// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
422+
/// }
423+
/// ```
333424
pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
334425
{
335426
let mut it = self.mut_iter();

0 commit comments

Comments
 (0)