Trait cantrip::List

source ·
pub trait List<Item> {
    // Required methods
    fn first(&self) -> Option<&Item>;
    fn last(&self) -> Option<&Item>;
    fn repeat(self, n: usize) -> Self
       where Item: Clone;
}
Expand description

List operations.

Methods have the following properties:

  • Requires the collection to represent a list
  • Does not consume the list or its elements
  • Does not create a new list

Required Methods§

source

fn first(&self) -> Option<&Item>

Returns the first element of this sequence, or None if it is empty.

§Examples
use cantrip::*;
use std::collections::LinkedList;

let a = LinkedList::from([1, 2, 3]);

assert_eq!(a.first(), Some(&1));
source

fn last(&self) -> Option<&Item>

Returns the last element of this sequence, or None if it is empty.

§Examples
use cantrip::*;
use std::collections::LinkedList;

let a = LinkedList::from([1, 2, 3]);

assert_eq!(a.last(), Some(&3));
source

fn repeat(self, n: usize) -> Self
where Item: Clone,

Creates a new collection by repeating this collection specified number of times.

§Example
use cantrip::*;
use std::collections::LinkedList;

let a = LinkedList::from([1, 2, 3]);

assert_eq!(a.repeat(2), LinkedList::from([1, 2, 3, 1, 2, 3]));
assert_eq!(a.repeat(0), LinkedList::new());

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<Item> List<Item> for LinkedList<Item>

source§

fn first(&self) -> Option<&Item>

source§

fn last(&self) -> Option<&Item>

source§

fn repeat(self, n: usize) -> Self
where Item: Clone,

source§

impl<Item> List<Item> for VecDeque<Item>

source§

fn first(&self) -> Option<&Item>

source§

fn last(&self) -> Option<&Item>

source§

fn repeat(self, n: usize) -> Self
where Item: Clone,

Implementors§