pub struct LinkedList<T, A =Global>where A:Allocator,{/* private fields */ }Expand description
A doubly-linked list with owned nodes.
TheLinkedList allows pushing and popping elements at either endin constant time.
ALinkedList with a known list of items can be initialized from an array:
NOTE: It is almost always better to useVec orVecDeque becausearray-based containers are generally faster,more memory efficient, and make better use of CPU cache.
Implementations§
Source§impl<T>LinkedList<T>
impl<T>LinkedList<T>
1.0.0 (const: 1.39.0) ·Sourcepub const fnnew() ->LinkedList<T>
pub const fnnew() ->LinkedList<T>
Creates an emptyLinkedList.
§Examples
1.0.0 ·Sourcepub fnappend(&mut self, other: &mutLinkedList<T>)
pub fnappend(&mut self, other: &mutLinkedList<T>)
Moves all elements fromother to the end of the list.
This reuses all the nodes fromother and moves them intoself. Afterthis operation,other becomes empty.
This operation should compute inO(1) time andO(1) memory.
§Examples
usestd::collections::LinkedList;letmutlist1 = LinkedList::new();list1.push_back('a');letmutlist2 = LinkedList::new();list2.push_back('b');list2.push_back('c');list1.append(&mutlist2);letmutiter = list1.iter();assert_eq!(iter.next(),Some(&'a'));assert_eq!(iter.next(),Some(&'b'));assert_eq!(iter.next(),Some(&'c'));assert!(iter.next().is_none());assert!(list2.is_empty());Source§impl<T, A>LinkedList<T, A>where A:Allocator,
impl<T, A>LinkedList<T, A>where A:Allocator,
Sourcepub const fnnew_in(alloc: A) ->LinkedList<T, A>
🔬This is a nightly-only experimental API. (allocator_api #32838)
pub const fnnew_in(alloc: A) ->LinkedList<T, A>
allocator_api #32838)Constructs an emptyLinkedList<T, A>.
§Examples
1.0.0 ·Sourcepub fniter_mut(&mut self) ->IterMut<'_, T>ⓘ
pub fniter_mut(&mut self) ->IterMut<'_, T>ⓘ
Provides a forward iterator with mutable references.
§Examples
usestd::collections::LinkedList;letmutlist: LinkedList<u32> = LinkedList::new();list.push_back(0);list.push_back(1);list.push_back(2);forelementinlist.iter_mut() {*element +=10;}letmutiter = 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);Sourcepub fncursor_front(&self) ->Cursor<'_, T, A>
🔬This is a nightly-only experimental API. (linked_list_cursors #58533)
pub fncursor_front(&self) ->Cursor<'_, T, A>
linked_list_cursors #58533)Provides a cursor at the front element.
The cursor is pointing to the “ghost” non-element if the list is empty.
Sourcepub fncursor_front_mut(&mut self) ->CursorMut<'_, T, A>
🔬This is a nightly-only experimental API. (linked_list_cursors #58533)
pub fncursor_front_mut(&mut self) ->CursorMut<'_, T, A>
linked_list_cursors #58533)Provides a cursor with editing operations at the front element.
The cursor is pointing to the “ghost” non-element if the list is empty.
Sourcepub fncursor_back(&self) ->Cursor<'_, T, A>
🔬This is a nightly-only experimental API. (linked_list_cursors #58533)
pub fncursor_back(&self) ->Cursor<'_, T, A>
linked_list_cursors #58533)Provides a cursor at the back element.
The cursor is pointing to the “ghost” non-element if the list is empty.
Sourcepub fncursor_back_mut(&mut self) ->CursorMut<'_, T, A>
🔬This is a nightly-only experimental API. (linked_list_cursors #58533)
pub fncursor_back_mut(&mut self) ->CursorMut<'_, T, A>
linked_list_cursors #58533)Provides a cursor with editing operations at the back element.
The cursor is pointing to the “ghost” non-element if the list is empty.
1.0.0 ·Sourcepub fnis_empty(&self) ->bool
pub fnis_empty(&self) ->bool
1.0.0 ·Sourcepub fnlen(&self) ->usize
pub fnlen(&self) ->usize
1.0.0 ·Sourcepub fnclear(&mut self)
pub fnclear(&mut self)
1.12.0 ·Sourcepub fncontains(&self, x:&T) ->boolwhere T:PartialEq,
pub fncontains(&self, x:&T) ->boolwhere T:PartialEq,
Returnstrue if theLinkedList contains an element equal to thegiven value.
This operation should compute linearly inO(n) time.
§Examples
1.0.0 ·Sourcepub fnfront(&self) ->Option<&T>
pub fnfront(&self) ->Option<&T>
Provides a reference to the front element, orNone if the list isempty.
This operation should compute inO(1) time.
§Examples
1.0.0 ·Sourcepub fnfront_mut(&mut self) ->Option<&mut T>
pub fnfront_mut(&mut self) ->Option<&mut T>
Provides a mutable reference to the front element, orNone if the listis empty.
This operation should compute inO(1) time.
§Examples
1.0.0 ·Sourcepub fnback(&self) ->Option<&T>
pub fnback(&self) ->Option<&T>
Provides a reference to the back element, orNone if the list isempty.
This operation should compute inO(1) time.
§Examples
1.0.0 ·Sourcepub fnback_mut(&mut self) ->Option<&mut T>
pub fnback_mut(&mut self) ->Option<&mut T>
Provides a mutable reference to the back element, orNone if the listis empty.
This operation should compute inO(1) time.
§Examples
1.0.0 ·Sourcepub fnpush_front(&mut self, elt: T)
pub fnpush_front(&mut self, elt: T)
Sourcepub fnpush_front_mut(&mut self, elt: T) ->&mut T
🔬This is a nightly-only experimental API. (push_mut #135974)
pub fnpush_front_mut(&mut self, elt: T) ->&mut T
push_mut #135974)Adds an element to the front of the list, returning a reference to it.
This operation should compute inO(1) time.
§Examples
1.0.0 ·Sourcepub fnpop_front(&mut self) ->Option<T>
pub fnpop_front(&mut self) ->Option<T>
Removes the first element and returns it, orNone if the list isempty.
This operation should compute inO(1) time.
§Examples
1.0.0 ·Sourcepub fnpush_back(&mut self, elt: T)
pub fnpush_back(&mut self, elt: T)
Sourcepub fnpush_back_mut(&mut self, elt: T) ->&mut T
🔬This is a nightly-only experimental API. (push_mut #135974)
pub fnpush_back_mut(&mut self, elt: T) ->&mut T
push_mut #135974)Adds an element to the back of the list, returning a reference to it.
This operation should compute inO(1) time.
§Examples
1.0.0 ·Sourcepub fnpop_back(&mut self) ->Option<T>
pub fnpop_back(&mut self) ->Option<T>
Removes the last element from a list and returns it, orNone ifit is empty.
This operation should compute inO(1) time.
§Examples
1.0.0 ·Sourcepub fnsplit_off(&mut self, at:usize) ->LinkedList<T, A>where A:Clone,
pub fnsplit_off(&mut self, at:usize) ->LinkedList<T, A>where A:Clone,
Sourcepub fnremove(&mut self, at:usize) -> T
🔬This is a nightly-only experimental API. (linked_list_remove #69210)
pub fnremove(&mut self, at:usize) -> T
linked_list_remove #69210)Sourcepub fnretain<F>(&mut self, f: F)
🔬This is a nightly-only experimental API. (linked_list_retain #114135)
pub fnretain<F>(&mut self, f: F)
linked_list_retain #114135)Retains only the elements specified by the predicate.
In other words, remove all elementse for whichf(&mut e) returns false.This method operates in place, visiting each element exactly once in theoriginal order, and preserves the order of the retained elements.
§Examples
#![feature(linked_list_retain)]usestd::collections::LinkedList;letmutd = LinkedList::new();d.push_front(1);d.push_front(2);d.push_front(3);d.retain(|&mutx| x %2==0);assert_eq!(d.pop_front(),Some(2));assert_eq!(d.pop_front(),None);Because the elements are visited exactly once in the original order,external state may be used to decide which elements to keep.
1.87.0 ·Sourcepub fnextract_if<F>(&mut self, filter: F) ->ExtractIf<'_, T, F, A>ⓘ
pub fnextract_if<F>(&mut self, filter: F) ->ExtractIf<'_, T, F, A>ⓘ
Creates an iterator which uses a closure to determine if an element should be removed.
If the closure returnstrue, the element is removed from the list andyielded. If the closure returnsfalse, or panics, the element remainsin the list and will not be yielded.
If the returnedExtractIf is not exhausted, e.g. because it is dropped without iteratingor the iteration short-circuits, then the remaining elements will be retained.Useextract_if().for_each(drop) if you do not need the returned iterator.
The iterator also lets you mutate the value of each element in theclosure, regardless of whether you choose to keep or remove it.
§Examples
Splitting a list into even and odd values, reusing the original list:
usestd::collections::LinkedList;letmutnumbers: LinkedList<u32> = LinkedList::new();numbers.extend(&[1,2,3,4,5,6,8,9,11,13,14,15]);letevens = numbers.extract_if(|x|*x %2==0).collect::<LinkedList<_>>();letodds = numbers;assert_eq!(evens.into_iter().collect::<Vec<_>>(),vec![2,4,6,8,14]);assert_eq!(odds.into_iter().collect::<Vec<_>>(),vec![1,3,5,9,11,13,15]);Trait Implementations§
1.0.0 ·Source§impl<T, A>Clone forLinkedList<T, A>
impl<T, A>Clone forLinkedList<T, A>
Source§fnclone_from(&mut self, source: &LinkedList<T, A>)
fnclone_from(&mut self, source: &LinkedList<T, A>)
Overwrites the contents ofself with a clone of the contents ofsource.
This method is preferred over simply assigningsource.clone() toself,as it avoids reallocation of the nodes of the linked list. Additionally,if the element typeT overridesclone_from(), this will reuse theresources ofself’s elements as well.
Source§fnclone(&self) ->LinkedList<T, A>
fnclone(&self) ->LinkedList<T, A>
1.0.0 ·Source§impl<T, A>Debug forLinkedList<T, A>
impl<T, A>Debug forLinkedList<T, A>
1.0.0 ·Source§impl<T>Default forLinkedList<T>
impl<T>Default forLinkedList<T>
Source§fndefault() ->LinkedList<T>
fndefault() ->LinkedList<T>
Creates an emptyLinkedList<T>.
1.0.0 ·Source§impl<T, A>Drop forLinkedList<T, A>where A:Allocator,
impl<T, A>Drop forLinkedList<T, A>where A:Allocator,
1.2.0 ·Source§impl<'a, T, A>Extend<&'a T> forLinkedList<T, A>
impl<'a, T, A>Extend<&'a T> forLinkedList<T, A>
1.0.0 ·Source§impl<T, A>Extend<T> forLinkedList<T, A>where A:Allocator,
impl<T, A>Extend<T> forLinkedList<T, A>where A:Allocator,
Source§fnextend<I>(&mut self, iter: I)where I:IntoIterator<Item = T>,
fnextend<I>(&mut self, iter: I)where I:IntoIterator<Item = T>,
Source§fnextend_one(&mut self, elem: T)
fnextend_one(&mut self, elem: T)
extend_one #72631)1.56.0 ·Source§impl<T, const N:usize>From<[T; N]> forLinkedList<T>
impl<T, const N:usize>From<[T; N]> forLinkedList<T>
Source§fnfrom(arr:[T; N]) ->LinkedList<T>
fnfrom(arr:[T; N]) ->LinkedList<T>
1.0.0 ·Source§impl<T>FromIterator<T> forLinkedList<T>
impl<T>FromIterator<T> forLinkedList<T>
Source§fnfrom_iter<I>(iter: I) ->LinkedList<T>where I:IntoIterator<Item = T>,
fnfrom_iter<I>(iter: I) ->LinkedList<T>where I:IntoIterator<Item = T>,
1.0.0 ·Source§impl<T, A>Hash forLinkedList<T, A>
impl<T, A>Hash forLinkedList<T, A>
1.0.0 ·Source§impl<'a, T, A>IntoIterator for &'aLinkedList<T, A>where A:Allocator,
impl<'a, T, A>IntoIterator for &'aLinkedList<T, A>where A:Allocator,
1.0.0 ·Source§impl<'a, T, A>IntoIterator for &'a mutLinkedList<T, A>where A:Allocator,
impl<'a, T, A>IntoIterator for &'a mutLinkedList<T, A>where A:Allocator,
1.0.0 ·Source§impl<T, A>IntoIterator forLinkedList<T, A>where A:Allocator,
impl<T, A>IntoIterator forLinkedList<T, A>where A:Allocator,
1.0.0 ·Source§impl<T, A>Ord forLinkedList<T, A>
impl<T, A>Ord forLinkedList<T, A>
1.0.0 ·Source§impl<T, A>PartialEq forLinkedList<T, A>
impl<T, A>PartialEq forLinkedList<T, A>
Source§fneq(&self, other: &LinkedList<T, A>) ->bool
fneq(&self, other: &LinkedList<T, A>) ->bool
self andother values to be equal, and is used by==.Source§fnne(&self, other: &LinkedList<T, A>) ->bool
fnne(&self, other: &LinkedList<T, A>) ->bool
!=. The default implementation is almost always sufficient,and should not be overridden without very good reason.