pub struct Cons { /* private fields */ }
Expand description
A Lisp “cons cell”.
A cons cell is similiar to a two-element tuple in Rust. Its fields
are traditionally called car
and cdr
, for obscure historical
reasons. Both the car
and the cdr
field can hold any Value
,
including other cons cells.
This data type is used to represent singly-linked lists, by
forming a chain of cons cells where the list element is kept in
the car
field, and the cdr
field either points to the next
cons cell, or terminates the list with any other value. Usually,
that terminator value is Value::Null
, also referred to as the
empty list. If any other terminating value is used, the resulting
linked list is referred to as “dotted”, or “improper” list.
The Cons
data type provides some utility function for the
singly-linked list use case, such as iterating through the list or
converting the list to a vector. To account for the possibility of
dotted lists, the iterators and vector conversion functions have
slightly unusual types.
The most natural way to traverse a singly linked list is probably by using
the list_iter
method.
Implementations§
source§impl Cons
impl Cons
sourcepub fn new<T, U>(car: T, cdr: U) -> Selfwhere
T: Into<Value>,
U: Into<Value>,
pub fn new<T, U>(car: T, cdr: U) -> Selfwhere T: Into<Value>, U: Into<Value>,
Constructs a new cons cell from two values.
sourcepub fn car_mut(&mut self) -> &mut Value
pub fn car_mut(&mut self) -> &mut Value
Returns a mutable reference to the value in the car
field.
sourcepub fn cdr_mut(&mut self) -> &mut Value
pub fn cdr_mut(&mut self) -> &mut Value
Returns a mutable reference to the value in the cdr
field.
sourcepub fn as_pair(&self) -> (&Value, &Value)
pub fn as_pair(&self) -> (&Value, &Value)
Returns references to the values in the car
and cdr
fields.
let cell = Cons::new(1, 2);
assert_eq!(cell.as_pair(), (&Value::from(1), &Value::from(2)));
sourcepub fn into_pair(self) -> (Value, Value)
pub fn into_pair(self) -> (Value, Value)
Converts self
into a pair of values without cloning.
let cell = Cons::new("a", 42);
assert_eq!(cell.car(), "a");
assert_eq!(cell.cdr(), 42);
let (car, cdr) = cell.into_pair();
assert_eq!(car, "a");
assert_eq!(cdr, 42);
sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Obtains an iterator yielding references to all the cons cells in this linked list.
for cell in Cons::new(1, Cons::new(2, Value::Null)).iter() {
println!("list element: {}", cell.car());
}
sourcepub fn into_vec(self) -> (Vec<Value>, Value)
pub fn into_vec(self) -> (Vec<Value>, Value)
Converts self
into a vector without cloning the elements.
Returns the accumulated items of the list and the cdr
of the last list
element. For proper lists, this will always be Value::Null
.
let list = Cons::new(1, Cons::new(2, Cons::new(3, Value::Null)));
assert_eq!(list.into_vec(), (vec![Value::from(1), Value::from(2), Value::from(3)], Value::Null));
sourcepub fn to_vec(&self) -> (Vec<Value>, Value)
pub fn to_vec(&self) -> (Vec<Value>, Value)
Retrieves a vector, cloning the values.
Returns the accumulated items of the list and the cdr
of the last list
element. For proper lists, this will always be Value::Null
.
let list = Cons::new(1, Cons::new(2, Cons::new(3, Value::Null)));
assert_eq!(list.to_vec(), (vec![Value::from(1), Value::from(2), Value::from(3)], Value::Null));
sourcepub fn to_ref_vec(&self) -> (Vec<&Value>, &Value)
pub fn to_ref_vec(&self) -> (Vec<&Value>, &Value)
Retrieves a vector, taking references to the values.
Returns the accumulated items of the list and the cdr
of the last list
element. For proper lists, this will always be Value::Null
.
let list = Cons::new(1, Cons::new(2, Cons::new(3, Value::Null)));
assert_eq!(list.to_ref_vec(), (vec![&Value::from(1), &Value::from(2), &Value::from(3)], &Value::Null));
sourcepub fn list_iter(&self) -> ListIter<'_> ⓘ
pub fn list_iter(&self) -> ListIter<'_> ⓘ
Returns an iterator that returns each element (car
field) of a singly-linked list.
The iterator returns None
if a terminating value is encountered. For a
dotted list, the iterator is not yet exhausted at that point, and
produces the non-Null
terminating value next.
Trait Implementations§
source§impl<'a> IntoIterator for &'a Cons
impl<'a> IntoIterator for &'a Cons
source§impl IntoIterator for Cons
impl IntoIterator for Cons
source§fn into_iter(self) -> IntoIter ⓘ
fn into_iter(self) -> IntoIter ⓘ
Obtains an iterator yielding the contents of the elements of this linked list.
The returned iterator transfers ownership of the values contained in the
list to the consumer of the iterator. For each cons cell but the last,
the iterator yields a pair containing the value in the cell’s car
field and None
. For the last cell, the yielded pair will contain the
value of car
and Some(cdr)
.
let vec: Vec<_> = Cons::new(1, Cons::new(2, 3)).into_iter().collect();
assert_eq!(vec, vec![(Value::from(1), None), (Value::from(2), Some(Value::from(3)))]);