@@ -278,6 +278,23 @@ impl<T> DList<T> {
278
278
/// Move the last element to the front of the list.
279
279
///
280
280
/// 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
+ /// ```
281
298
#[ inline]
282
299
pub fn rotate_forward ( & mut self ) {
283
300
self . pop_back_node ( ) . map ( |tail| {
@@ -288,6 +305,23 @@ impl<T> DList<T> {
288
305
/// Move the first element to the back of the list.
289
306
///
290
307
/// 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
+ /// ```
291
325
#[ inline]
292
326
pub fn rotate_backward ( & mut self ) {
293
327
self . pop_front_node ( ) . map ( |head| {
@@ -298,6 +332,25 @@ impl<T> DList<T> {
298
332
/// Add all elements from `other` to the end of the list
299
333
///
300
334
/// 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
+ /// ```
301
354
pub fn append ( & mut self , mut other : DList < T > ) {
302
355
match self . list_tail . resolve ( ) {
303
356
None => * self = other,
@@ -320,6 +373,25 @@ impl<T> DList<T> {
320
373
/// Add all elements from `other` to the beginning of the list
321
374
///
322
375
/// 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
+ /// ```
323
395
#[ inline]
324
396
pub fn prepend ( & mut self , mut other : DList < T > ) {
325
397
mem:: swap ( self , & mut other) ;
@@ -330,6 +402,25 @@ impl<T> DList<T> {
330
402
/// or at the end.
331
403
///
332
404
/// 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
+ /// ```
333
424
pub fn insert_when ( & mut self , elt : T , f : |& T , & T | -> bool) {
334
425
{
335
426
let mut it = self . mut_iter ( ) ;
0 commit comments