[−][src]Struct serde_lexpr::Cons
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.
Methods
impl Cons
[src]
pub fn new<T, U>(car: T, cdr: U) -> Cons where
T: Into<Value>,
U: Into<Value>,
[src]
T: Into<Value>,
U: Into<Value>,
Constructs a new cons cell from two values.
pub fn car(&self) -> &Value
[src]
Returns a reference to the value in the car
field.
pub fn car_mut(&mut self) -> &mut Value
[src]
Returns a mutable reference to the value in the car
field.
pub fn set_car(&mut self, car: impl Into<Value>)
[src]
Sets the car
field.
pub fn cdr(&self) -> &Value
[src]
Returns a reference to the value in the cdr
field.
pub fn cdr_mut(&mut self) -> &mut Value
[src]
Returns a mutable reference to the value in the cdr
field.
pub fn set_cdr(&mut self, cdr: impl Into<Value>)
[src]
Sets the cdr
field.
pub fn as_pair(&self) -> (&Value, &Value)
[src]
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)));
pub fn into_pair(self) -> (Value, Value)
[src]
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);
pub fn iter(&self) -> Iter
[src]
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()); }
pub fn into_vec(self) -> (Vec<Value>, Value)
[src]
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));
pub fn to_vec(&self) -> (Vec<Value>, Value)
[src]
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));
pub fn to_ref_vec(&self) -> (Vec<&Value>, &Value)
[src]
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));
pub fn list_iter(&self) -> ListIter
[src]
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
impl Clone for Cons
[src]
impl Debug for Cons
[src]
impl From<Cons> for Value
[src]
impl IntoIterator for Cons
[src]
type Item = (Value, Option<Value>)
The type of the elements being iterated over.
type IntoIter = IntoIter
Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter
[src]
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)))]);
impl<'a> IntoIterator for &'a Cons
[src]
type Item = &'a Cons
The type of the elements being iterated over.
type IntoIter = Iter<'a>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a>
[src]
impl PartialEq<Cons> for Cons
[src]
impl StructuralPartialEq for Cons
[src]
Auto Trait Implementations
impl RefUnwindSafe for Cons
impl Send for Cons
impl Sync for Cons
impl Unpin for Cons
impl UnwindSafe for Cons
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<I> IntoIterator for I where
I: Iterator,
[src]
I: Iterator,
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
fn into_iter(self) -> I
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,