List
class List<T> Import
Source: use collections::list::{List}; src/collections/list.hy A singly-linked list with constant-time push and pop at the front.
push_back and pop_back walk the whole list, so use
collections::deque::VecDeque when you need both ends to be fast.
for x in list visits the values front to back.
Associated functions#
| Name | Description |
|---|---|
new | Creates an empty list. |
Methods#
| Name | Description |
|---|---|
size | Returns the number of values. |
is_empty | Returns true when the list has no values. |
push_front | Adds v at the front. |
push_back | Adds v at the back. |
peek_front | Returns the front value without removing it, or None when the list is empty. |
pop_front | Removes and returns the front value, or None when the list is empty. |
pop_back | Removes and returns the back value, or None when the list is empty. |
clear | Removes every value. |
to_vec | Returns the values front to back as a new Vec. |
iter | Returns an iterator over the values, front to back. |
Trait implementations#
IntoIteratorforList<T>
Invariants#
Checked according to --contracts (see Contracts).
invariant self.len >= 0
Examples#
use collections::list::{List};
use tests::assert;
let xs: List<int> = List::new();
xs.push_front(2);
xs.push_front(1);
xs.push_back(3);
assert!(xs.size() == 3);
let sum = 0;
for x in xs {
sum = sum * 10 + x;
}
assert!(sum == 123);
assert!((xs.pop_front() ?? 0) == 1);
See also#
List::push_backList::pop_backcollections::deque::VecDequeNode: One cell of aList: a value and the rest of the list.ListIter: An iterator over aList, front to back, fromList::iter.