Movatterモバイル変換


[0]ホーム

URL:


LinkedList

std::collections

StructLinkedList 

1.0.0 ·Source
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:

usestd::collections::LinkedList;letlist = LinkedList::from([1,2,3]);

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>

1.0.0 (const: 1.39.0) ·Source

pub const fnnew() ->LinkedList<T>

Creates an emptyLinkedList.

§Examples
usestd::collections::LinkedList;letlist: LinkedList<u32> = LinkedList::new();
1.0.0 ·Source

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,

Source

pub const fnnew_in(alloc: A) ->LinkedList<T, A>

🔬This is a nightly-only experimental API. (allocator_api #32838)

Constructs an emptyLinkedList<T, A>.

§Examples
#![feature(allocator_api)]usestd::alloc::System;usestd::collections::LinkedList;letlist: LinkedList<u32,_> = LinkedList::new_in(System);
1.0.0 ·Source

pub fniter(&self) ->Iter<'_, T>

Provides a forward iterator.

§Examples
usestd::collections::LinkedList;letmutlist: LinkedList<u32> = LinkedList::new();list.push_back(0);list.push_back(1);list.push_back(2);letmutiter = list.iter();assert_eq!(iter.next(),Some(&0));assert_eq!(iter.next(),Some(&1));assert_eq!(iter.next(),Some(&2));assert_eq!(iter.next(),None);
1.0.0 ·Source

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);
Source

pub fncursor_front(&self) ->Cursor<'_, T, A>

🔬This is a nightly-only experimental API. (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.

Source

pub fncursor_front_mut(&mut self) ->CursorMut<'_, T, A>

🔬This is a nightly-only experimental API. (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.

Source

pub fncursor_back(&self) ->Cursor<'_, T, A>

🔬This is a nightly-only experimental API. (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.

Source

pub fncursor_back_mut(&mut self) ->CursorMut<'_, T, A>

🔬This is a nightly-only experimental API. (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 ·Source

pub fnis_empty(&self) ->bool

Returnstrue if theLinkedList is empty.

This operation should compute inO(1) time.

§Examples
usestd::collections::LinkedList;letmutdl = LinkedList::new();assert!(dl.is_empty());dl.push_front("foo");assert!(!dl.is_empty());
1.0.0 ·Source

pub fnlen(&self) ->usize

Returns the length of theLinkedList.

This operation should compute inO(1) time.

§Examples
usestd::collections::LinkedList;letmutdl = LinkedList::new();dl.push_front(2);assert_eq!(dl.len(),1);dl.push_front(1);assert_eq!(dl.len(),2);dl.push_back(3);assert_eq!(dl.len(),3);
1.0.0 ·Source

pub fnclear(&mut self)

Removes all elements from theLinkedList.

This operation should compute inO(n) time.

§Examples
usestd::collections::LinkedList;letmutdl = LinkedList::new();dl.push_front(2);dl.push_front(1);assert_eq!(dl.len(),2);assert_eq!(dl.front(),Some(&1));dl.clear();assert_eq!(dl.len(),0);assert_eq!(dl.front(),None);
1.12.0 ·Source

pub fncontains(&self, x:&T) ->bool
where T:PartialEq,

Returnstrue if theLinkedList contains an element equal to thegiven value.

This operation should compute linearly inO(n) time.

§Examples
usestd::collections::LinkedList;letmutlist: 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);
1.0.0 ·Source

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
usestd::collections::LinkedList;letmutdl = LinkedList::new();assert_eq!(dl.front(),None);dl.push_front(1);assert_eq!(dl.front(),Some(&1));
1.0.0 ·Source

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
usestd::collections::LinkedList;letmutdl = LinkedList::new();assert_eq!(dl.front(),None);dl.push_front(1);assert_eq!(dl.front(),Some(&1));matchdl.front_mut() {None=> {},Some(x) =>*x =5,}assert_eq!(dl.front(),Some(&5));
1.0.0 ·Source

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
usestd::collections::LinkedList;letmutdl = LinkedList::new();assert_eq!(dl.back(),None);dl.push_back(1);assert_eq!(dl.back(),Some(&1));
1.0.0 ·Source

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
usestd::collections::LinkedList;letmutdl = LinkedList::new();assert_eq!(dl.back(),None);dl.push_back(1);assert_eq!(dl.back(),Some(&1));matchdl.back_mut() {None=> {},Some(x) =>*x =5,}assert_eq!(dl.back(),Some(&5));
1.0.0 ·Source

pub fnpush_front(&mut self, elt: T)

Adds an element to the front of the list.

This operation should compute inO(1) time.

§Examples
usestd::collections::LinkedList;letmutdl = LinkedList::new();dl.push_front(2);assert_eq!(dl.front().unwrap(),&2);dl.push_front(1);assert_eq!(dl.front().unwrap(),&1);
Source

pub fnpush_front_mut(&mut self, elt: T) ->&mut T

🔬This is a nightly-only experimental API. (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
#![feature(push_mut)]usestd::collections::LinkedList;letmutdl = LinkedList::from([1,2,3]);letptr = dl.push_front_mut(2);*ptr +=4;assert_eq!(dl.front().unwrap(),&6);
1.0.0 ·Source

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
usestd::collections::LinkedList;letmutd = LinkedList::new();assert_eq!(d.pop_front(),None);d.push_front(1);d.push_front(3);assert_eq!(d.pop_front(),Some(3));assert_eq!(d.pop_front(),Some(1));assert_eq!(d.pop_front(),None);
1.0.0 ·Source

pub fnpush_back(&mut self, elt: T)

Adds an element to the back of the list.

This operation should compute inO(1) time.

§Examples
usestd::collections::LinkedList;letmutd = LinkedList::new();d.push_back(1);d.push_back(3);assert_eq!(3,*d.back().unwrap());
Source

pub fnpush_back_mut(&mut self, elt: T) ->&mut T

🔬This is a nightly-only experimental API. (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
#![feature(push_mut)]usestd::collections::LinkedList;letmutdl = LinkedList::from([1,2,3]);letptr = dl.push_back_mut(2);*ptr +=4;assert_eq!(dl.back().unwrap(),&6);
1.0.0 ·Source

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
usestd::collections::LinkedList;letmutd = LinkedList::new();assert_eq!(d.pop_back(),None);d.push_back(1);d.push_back(3);assert_eq!(d.pop_back(),Some(3));
1.0.0 ·Source

pub fnsplit_off(&mut self, at:usize) ->LinkedList<T, A>
where A:Clone,

Splits the list into two at the given index. Returns everything after the given index,including the index.

This operation should compute inO(n) time.

§Panics

Panics ifat > len.

§Examples
usestd::collections::LinkedList;letmutd = LinkedList::new();d.push_front(1);d.push_front(2);d.push_front(3);letmutsplit = d.split_off(2);assert_eq!(split.pop_front(),Some(1));assert_eq!(split.pop_front(),None);
Source

pub fnremove(&mut self, at:usize) -> T

🔬This is a nightly-only experimental API. (linked_list_remove #69210)

Removes the element at the given index and returns it.

This operation should compute inO(n) time.

§Panics

Panics if at >= len

§Examples
#![feature(linked_list_remove)]usestd::collections::LinkedList;letmutd = LinkedList::new();d.push_front(1);d.push_front(2);d.push_front(3);assert_eq!(d.remove(1),2);assert_eq!(d.remove(0),3);assert_eq!(d.remove(0),1);
Source

pub fnretain<F>(&mut self, f: F)
where F:FnMut(&mut T) ->bool,

🔬This is a nightly-only experimental API. (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.

#![feature(linked_list_retain)]usestd::collections::LinkedList;letmutd = LinkedList::new();d.push_front(1);d.push_front(2);d.push_front(3);letkeep = [false,true,false];letmutiter = keep.iter();d.retain(|_|*iter.next().unwrap());assert_eq!(d.pop_front(),Some(2));assert_eq!(d.pop_front(),None);
1.87.0 ·Source

pub fnextract_if<F>(&mut self, filter: F) ->ExtractIf<'_, T, F, A>
where F:FnMut(&mut T) ->bool,

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>
where T:Clone, A:Allocator +Clone,

Source§

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>

Returns a duplicate of the value.Read more
1.0.0 ·Source§

impl<T, A>Debug forLinkedList<T, A>
where T:Debug, A:Allocator,

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>

Formats the value using the given formatter.Read more
1.0.0 ·Source§

impl<T>Default forLinkedList<T>

Source§

fndefault() ->LinkedList<T>

Creates an emptyLinkedList<T>.

1.0.0 ·Source§

impl<T, A>Drop forLinkedList<T, A>
where A:Allocator,

Source§

fndrop(&mut self)

Executes the destructor for this type.Read more
1.2.0 ·Source§

impl<'a, T, A>Extend<&'a T> forLinkedList<T, A>
where T: 'a +Copy, A:Allocator,

Source§

fnextend<I>(&mut self, iter: I)
where I:IntoIterator<Item =&'a T>,

Extends a collection with the contents of an iterator.Read more
Source§

fnextend_one(&mut self, _:&'a T)

🔬This is a nightly-only experimental API. (extend_one #72631)
Extends a collection with exactly one element.
Source§

fnextend_reserve(&mut self, additional:usize)

🔬This is a nightly-only experimental API. (extend_one #72631)
Reserves capacity in a collection for the given number of additional elements.Read more
1.0.0 ·Source§

impl<T, A>Extend<T> forLinkedList<T, A>
where A:Allocator,

Source§

fnextend<I>(&mut self, iter: I)
where I:IntoIterator<Item = T>,

Extends a collection with the contents of an iterator.Read more
Source§

fnextend_one(&mut self, elem: T)

🔬This is a nightly-only experimental API. (extend_one #72631)
Extends a collection with exactly one element.
Source§

fnextend_reserve(&mut self, additional:usize)

🔬This is a nightly-only experimental API. (extend_one #72631)
Reserves capacity in a collection for the given number of additional elements.Read more
1.56.0 ·Source§

impl<T, const N:usize>From<[T; N]> forLinkedList<T>

Source§

fnfrom(arr:[T; N]) ->LinkedList<T>

Converts a[T; N] into aLinkedList<T>.

usestd::collections::LinkedList;letlist1 = LinkedList::from([1,2,3,4]);letlist2: LinkedList<_> = [1,2,3,4].into();assert_eq!(list1, list2);
1.0.0 ·Source§

impl<T>FromIterator<T> forLinkedList<T>

Source§

fnfrom_iter<I>(iter: I) ->LinkedList<T>
where I:IntoIterator<Item = T>,

Creates a value from an iterator.Read more
1.0.0 ·Source§

impl<T, A>Hash forLinkedList<T, A>
where T:Hash, A:Allocator,

Source§

fnhash<H>(&self, state:&mut H)
where H:Hasher,

Feeds this value into the givenHasher.Read more
1.3.0 ·Source§

fnhash_slice<H>(data: &[Self], state:&mut H)
where H:Hasher, Self:Sized,

Feeds a slice of this type into the givenHasher.Read more
1.0.0 ·Source§

impl<'a, T, A>IntoIterator for &'aLinkedList<T, A>
where A:Allocator,

Source§

typeItem =&'a T

The type of the elements being iterated over.
Source§

typeIntoIter =Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fninto_iter(self) ->Iter<'a, T>

Creates an iterator from a value.Read more
1.0.0 ·Source§

impl<'a, T, A>IntoIterator for &'a mutLinkedList<T, A>
where A:Allocator,

Source§

typeItem =&'a mut T

The type of the elements being iterated over.
Source§

typeIntoIter =IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fninto_iter(self) ->IterMut<'a, T>

Creates an iterator from a value.Read more
1.0.0 ·Source§

impl<T, A>IntoIterator forLinkedList<T, A>
where A:Allocator,

Source§

fninto_iter(self) ->IntoIter<T, A>

Consumes the list into an iterator yielding elements by value.

Source§

typeItem = T

The type of the elements being iterated over.
Source§

typeIntoIter =IntoIter<T, A>

Which kind of iterator are we turning this into?
1.0.0 ·Source§

impl<T, A>Ord forLinkedList<T, A>
where T:Ord, A:Allocator,

Source§

fncmp(&self, other: &LinkedList<T, A>) ->Ordering

This method returns anOrdering betweenself andother.Read more
1.21.0 ·Source§

fnmax(self, other: Self) -> Self
where Self:Sized,

Compares and returns the maximum of two values.Read more
1.21.0 ·Source§

fnmin(self, other: Self) -> Self
where Self:Sized,

Compares and returns the minimum of two values.Read more
1.50.0 ·Source§

fnclamp(self, min: Self, max: Self) -> Self
where Self:Sized,

Restrict a value to a certain interval.Read more
1.0.0 ·Source§

impl<T, A>PartialEq forLinkedList<T, A>
where T:PartialEq, A:Allocator,

Source§

fneq(&self, other: &LinkedList<T, A>) ->bool

Tests forself andother values to be equal, and is used by==.
Source§

fnne(&self, other: &LinkedList<T, A>) ->bool

Tests for!=. The default implementation is almost always sufficient,and should not be overridden without very good reason.
1.0.0 ·Source§

impl<T, A>PartialOrd forLinkedList<T, A>
where T:PartialOrd, A:Allocator,

Source§

fnpartial_cmp(&self, other: &LinkedList<T, A>) ->Option<Ordering>

This method returns an ordering betweenself andother values if one exists.Read more
1.0.0 ·Source§

fnlt(&self, other:&Rhs) ->bool

Tests less than (forself andother) and is used by the< operator.Read more
1.0.0 ·Source§

fnle(&self, other:&Rhs) ->bool

Tests less than or equal to (forself andother) and is used by the<= operator.Read more
1.0.0 ·Source§

fngt(&self, other:&Rhs) ->bool

Tests greater than (forself andother) and is used by the>operator.Read more
1.0.0 ·Source§

fnge(&self, other:&Rhs) ->bool

Tests greater than or equal to (forself andother) and is used bythe>= operator.Read more
1.0.0 ·Source§

impl<T, A>Eq forLinkedList<T, A>
where T:Eq, A:Allocator,

1.0.0 ·Source§

impl<T, A>Send forLinkedList<T, A>
where T:Send, A:Allocator +Send,

1.0.0 ·Source§

impl<T, A>Sync forLinkedList<T, A>
where T:Sync, A:Allocator +Sync,

Auto Trait Implementations§

§

impl<T, A>Freeze forLinkedList<T, A>
where A:Freeze,

§

impl<T, A>RefUnwindSafe forLinkedList<T, A>

§

impl<T, A>Unpin forLinkedList<T, A>
where A:Unpin,

§

impl<T, A>UnwindSafe forLinkedList<T, A>

Blanket Implementations§

Source§

impl<T>Any for T
where T: 'static + ?Sized,

Source§

fntype_id(&self) ->TypeId

Gets theTypeId ofself.Read more
Source§

impl<T>Borrow<T> for T
where T: ?Sized,

Source§

fnborrow(&self) ->&T

Immutably borrows from an owned value.Read more
Source§

impl<T>BorrowMut<T> for T
where T: ?Sized,

Source§

fnborrow_mut(&mut self) ->&mut T

Mutably borrows from an owned value.Read more
Source§

impl<T>CloneToUninit for T
where T:Clone,

Source§

unsafe fnclone_to_uninit(&self, dest:*mutu8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment fromself todest.Read more
Source§

impl<T>From<T> for T

Source§

fnfrom(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U>Into<U> for T
where U:From<T>,

Source§

fninto(self) -> U

CallsU::from(self).

That is, this conversion is whatever the implementation ofFrom<T> for U chooses to do.

Source§

impl<T>ToOwned for T
where T:Clone,

Source§

typeOwned = T

The resulting type after obtaining ownership.
Source§

fnto_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning.Read more
Source§

fnclone_into(&self, target:&mut T)

Uses borrowed data to replace owned data, usually by cloning.Read more
Source§

impl<T, U>TryFrom<U> for T
where U:Into<T>,

Source§

typeError =Infallible

The type returned in the event of a conversion error.
Source§

fntry_from(value: U) ->Result<T, <T asTryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U>TryInto<U> for T
where U:TryFrom<T>,

Source§

typeError = <U asTryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fntry_into(self) ->Result<U, <U asTryFrom<T>>::Error>

Performs the conversion.

[8]ページ先頭

©2009-2025 Movatter.jp