Struct List

Source
pub struct List {
    pub _list: VecDeque<Object>,
}
Expand description

the main component

contens structure of good docs [short sentence explaining what it is]

[more detailed explanation]

[at least one code example that users can copy/paste to try it]

[even more advanced explanations if necessary]

Fields§

§_list: VecDeque<Object>

_list which holds all the python objects together

Implementations§

Source§

impl List

its implementation

Source

pub fn new() -> List

new function

Methods from Deref<Target = VecDeque<Object>>§

1.0.0 · Source

pub fn get(&self, index: usize) -> Option<&T>

Provides a reference to the element at the given index.

Element at index 0 is the front of the queue.

§Examples
use std::collections::VecDeque;

let mut buf = VecDeque::new();
buf.push_back(3);
buf.push_back(4);
buf.push_back(5);
buf.push_back(6);
assert_eq!(buf.get(1), Some(&4));
1.0.0 · Source

pub fn capacity(&self) -> usize

Returns the number of elements the deque can hold without reallocating.

§Examples
use std::collections::VecDeque;

let buf: VecDeque<i32> = VecDeque::with_capacity(10);
assert!(buf.capacity() >= 10);
Source

pub fn allocator(&self) -> &A

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

Returns a reference to the underlying allocator.

1.0.0 · Source

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

Returns a front-to-back iterator.

§Examples
use std::collections::VecDeque;

let mut buf = VecDeque::new();
buf.push_back(5);
buf.push_back(3);
buf.push_back(4);
let b: &[_] = &[&5, &3, &4];
let c: Vec<&i32> = buf.iter().collect();
assert_eq!(&c[..], b);
1.5.0 · Source

pub fn as_slices(&self) -> (&[T], &[T])

Returns a pair of slices which contain, in order, the contents of the deque.

If make_contiguous was previously called, all elements of the deque will be in the first slice and the second slice will be empty. Otherwise, the exact split point depends on implementation details and is not guaranteed.

§Examples
use std::collections::VecDeque;

let mut deque = VecDeque::new();

deque.push_back(0);
deque.push_back(1);
deque.push_back(2);

let expected = [0, 1, 2];
let (front, back) = deque.as_slices();
assert_eq!(&expected[..front.len()], front);
assert_eq!(&expected[front.len()..], back);

deque.push_front(10);
deque.push_front(9);

let expected = [9, 10, 0, 1, 2];
let (front, back) = deque.as_slices();
assert_eq!(&expected[..front.len()], front);
assert_eq!(&expected[front.len()..], back);
1.0.0 · Source

pub fn len(&self) -> usize

Returns the number of elements in the deque.

§Examples
use std::collections::VecDeque;

let mut deque = VecDeque::new();
assert_eq!(deque.len(), 0);
deque.push_back(1);
assert_eq!(deque.len(), 1);
1.0.0 · Source

pub fn is_empty(&self) -> bool

Returns true if the deque is empty.

§Examples
use std::collections::VecDeque;

let mut deque = VecDeque::new();
assert!(deque.is_empty());
deque.push_front(1);
assert!(!deque.is_empty());
1.51.0 · Source

pub fn range<R>(&self, range: R) -> Iter<'_, T>
where R: RangeBounds<usize>,

Creates an iterator that covers the specified range in the deque.

§Panics

Panics if the starting point is greater than the end point or if the end point is greater than the length of the deque.

§Examples
use std::collections::VecDeque;

let deque: VecDeque<_> = [1, 2, 3].into();
let range = deque.range(2..).copied().collect::<VecDeque<_>>();
assert_eq!(range, [3]);

// A full range covers all contents
let all = deque.range(..);
assert_eq!(all.len(), 3);
1.12.0 · Source

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

Returns true if the deque contains an element equal to the given value.

This operation is O(n).

Note that if you have a sorted VecDeque, binary_search may be faster.

§Examples
use std::collections::VecDeque;

let mut deque: VecDeque<u32> = VecDeque::new();

deque.push_back(0);
deque.push_back(1);

assert_eq!(deque.contains(&1), true);
assert_eq!(deque.contains(&10), false);
1.0.0 · Source

pub fn front(&self) -> Option<&T>

Provides a reference to the front element, or None if the deque is empty.

§Examples
use std::collections::VecDeque;

let mut d = VecDeque::new();
assert_eq!(d.front(), None);

d.push_back(1);
d.push_back(2);
assert_eq!(d.front(), Some(&1));
1.0.0 · Source

pub fn back(&self) -> Option<&T>

Provides a reference to the back element, or None if the deque is empty.

§Examples
use std::collections::VecDeque;

let mut d = VecDeque::new();
assert_eq!(d.back(), None);

d.push_back(1);
d.push_back(2);
assert_eq!(d.back(), Some(&2));

Binary searches this VecDeque for a given element. If the VecDeque is not sorted, the returned result is unspecified and meaningless.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

See also binary_search_by, binary_search_by_key, and partition_point.

§Examples

Looks up a series of four elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4].

use std::collections::VecDeque;

let deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();

assert_eq!(deque.binary_search(&13),  Ok(9));
assert_eq!(deque.binary_search(&4),   Err(7));
assert_eq!(deque.binary_search(&100), Err(13));
let r = deque.binary_search(&1);
assert!(matches!(r, Ok(1..=4)));

If you want to insert an item to a sorted deque, while maintaining sort order, consider using partition_point:

use std::collections::VecDeque;

let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
let num = 42;
let idx = deque.partition_point(|&x| x <= num);
// If `num` is unique, `s.partition_point(|&x| x < num)` (with `<`) is equivalent to
// `s.binary_search(&num).unwrap_or_else(|x| x)`, but using `<=` may allow `insert`
// to shift less elements.
deque.insert(idx, num);
assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
1.54.0 · Source

pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
where F: FnMut(&'a T) -> Ordering,

Binary searches this VecDeque with a comparator function.

The comparator function should return an order code that indicates whether its argument is Less, Equal or Greater the desired target. If the VecDeque is not sorted or if the comparator function does not implement an order consistent with the sort order of the underlying VecDeque, the returned result is unspecified and meaningless.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

See also binary_search, binary_search_by_key, and partition_point.

§Examples

Looks up a series of four elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4].

use std::collections::VecDeque;

let deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();

assert_eq!(deque.binary_search_by(|x| x.cmp(&13)),  Ok(9));
assert_eq!(deque.binary_search_by(|x| x.cmp(&4)),   Err(7));
assert_eq!(deque.binary_search_by(|x| x.cmp(&100)), Err(13));
let r = deque.binary_search_by(|x| x.cmp(&1));
assert!(matches!(r, Ok(1..=4)));
1.54.0 · Source

pub fn binary_search_by_key<'a, B, F>( &'a self, b: &B, f: F, ) -> Result<usize, usize>
where F: FnMut(&'a T) -> B, B: Ord,

Binary searches this VecDeque with a key extraction function.

Assumes that the deque is sorted by the key, for instance with make_contiguous().sort_by_key() using the same key extraction function. If the deque is not sorted by the key, the returned result is unspecified and meaningless.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

See also binary_search, binary_search_by, and partition_point.

§Examples

Looks up a series of four elements in a slice of pairs sorted by their second elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4].

use std::collections::VecDeque;

let deque: VecDeque<_> = [(0, 0), (2, 1), (4, 1), (5, 1),
         (3, 1), (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
         (1, 21), (2, 34), (4, 55)].into();

assert_eq!(deque.binary_search_by_key(&13, |&(a, b)| b),  Ok(9));
assert_eq!(deque.binary_search_by_key(&4, |&(a, b)| b),   Err(7));
assert_eq!(deque.binary_search_by_key(&100, |&(a, b)| b), Err(13));
let r = deque.binary_search_by_key(&1, |&(a, b)| b);
assert!(matches!(r, Ok(1..=4)));
1.54.0 · Source

pub fn partition_point<P>(&self, pred: P) -> usize
where P: FnMut(&T) -> bool,

Returns the index of the partition point according to the given predicate (the index of the first element of the second partition).

The deque is assumed to be partitioned according to the given predicate. This means that all elements for which the predicate returns true are at the start of the deque and all elements for which the predicate returns false are at the end. For example, [7, 15, 3, 5, 4, 12, 6] is partitioned under the predicate x % 2 != 0 (all odd numbers are at the start, all even at the end).

If the deque is not partitioned, the returned result is unspecified and meaningless, as this method performs a kind of binary search.

See also binary_search, binary_search_by, and binary_search_by_key.

§Examples
use std::collections::VecDeque;

let deque: VecDeque<_> = [1, 2, 3, 3, 5, 6, 7].into();
let i = deque.partition_point(|&x| x < 5);

assert_eq!(i, 4);
assert!(deque.iter().take(i).all(|&x| x < 5));
assert!(deque.iter().skip(i).all(|&x| !(x < 5)));

If you want to insert an item to a sorted deque, while maintaining sort order:

use std::collections::VecDeque;

let mut deque: VecDeque<_> = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55].into();
let num = 42;
let idx = deque.partition_point(|&x| x < num);
deque.insert(idx, num);
assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);

Trait Implementations§

Source§

impl Add for List

list concatenation

Source§

fn add(self, rhs: Self) -> Self::Output

list1 + list2 == list3

Source§

type Output = List

The resulting type after applying the + operator.
Source§

impl Append<&str> for List

Source§

fn append_back(&mut self, _str: &str) -> &mut Self

Performs append
Source§

impl Append<Bool> for List

Source§

fn append_back(&mut self, _bool: Bool) -> &mut Self

§append python bool: Bool struct

example

use python::*;
use pretty_assertions::assert_eq;

let python_bool = Bool::new(true);
let mut python_list = List::new();
python_list.append_back(python_bool);
print(python_list);
// assert_eq!(123, 1233);

output

[True]
Source§

impl Append<Float<f32>> for List

Source§

fn append_back(&mut self, _float: Float<f32>) -> &mut Self

Performs append
Source§

impl Append<Float<f64>> for List

Source§

fn append_back(&mut self, _float: Float<f64>) -> &mut Self

Performs append
Source§

impl Append<Int<i32>> for List

Source§

fn append_back(&mut self, _integer: Int<i32>) -> &mut Self

Performs append
Source§

impl Append<Int<i64>> for List

Source§

fn append_back(&mut self, _integer: Int<i64>) -> &mut Self

Performs append
Source§

impl Append<List> for List

Source§

fn append_back(&mut self, _list: List) -> &mut Self

Performs append
Source§

impl Append<Object> for List

Source§

fn append_back(&mut self, object: Object) -> &mut Self

Performs append
Source§

impl Append<String> for List

Source§

fn append_back(&mut self, string: String) -> &mut Self

Performs append
Source§

impl Append<_String> for List

Source§

fn append_back(&mut self, _string: _String) -> &mut Self

Performs append
Source§

impl Append<bool> for List

Source§

fn append_back(&mut self, _bool: bool) -> &mut Self

Performs append
Source§

impl Append<char> for List

Source§

fn append_back(&mut self, _char: char) -> &mut Self

Performs append
Source§

impl Append<f32> for List

Source§

fn append_back(&mut self, _float: f32) -> &mut Self

Performs append
Source§

impl Append<f64> for List

Source§

fn append_back(&mut self, _float: f64) -> &mut Self

Performs append
Source§

impl Append<i32> for List

inline append for integer example

let mut one_elem = List::new(); one_elem .append_back(123) .append_back(123) .append_back(123) .append_back(123) .append_back(123) .append_back(123) .append_back(123); println!(“{}”, one_elem);

[123, 123, 123, 123, 123, 123, 123]

Source§

fn append_back(&mut self, _integer: i32) -> &mut Self

Performs append
Source§

impl Append<i64> for List

Source§

fn append_back(&mut self, _integer: i64) -> &mut Self

Performs append
Source§

impl AppendFront<&str> for List

Source§

fn append_front(&mut self, _str: &str) -> &mut Self

performs the append front
Source§

impl AppendFront<Bool> for List

Source§

fn append_front(&mut self, _bool: Bool) -> &mut Self

§append python bool: Bool struct

example

use python::*;
use pretty_assertions::assert_eq;

let python_bool = Bool::new(true);
let mut python_list = List::new();
python_list.append_back(python_bool);
print(python_list);
// assert_eq!(123, 1233);

output

[True]
Source§

impl AppendFront<List> for List

Source§

fn append_front(&mut self, _list: List) -> &mut Self

performs the append front
Source§

impl AppendFront<String> for List

Source§

fn append_front(&mut self, string: String) -> &mut Self

performs the append front
Source§

impl AppendFront<_String> for List

Source§

fn append_front(&mut self, _string: _String) -> &mut Self

performs the append front
Source§

impl AppendFront<bool> for List

Source§

fn append_front(&mut self, _bool: bool) -> &mut Self

performs the append front
Source§

impl AppendFront<char> for List

Source§

fn append_front(&mut self, _char: char) -> &mut Self

performs the append front
Source§

impl AppendFront<f32> for List

Source§

fn append_front(&mut self, _float: f32) -> &mut Self

performs the append front
Source§

impl AppendFront<f64> for List

Source§

fn append_front(&mut self, _float: f64) -> &mut Self

performs the append front
Source§

impl AppendFront<i32> for List

Source§

fn append_front(&mut self, _integer: i32) -> &mut Self

performs the append front
Source§

impl Debug for List

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl Default for List

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Deref for List

Source§

fn deref(&self) -> &Self::Target

usage for o in python_list.iter() { print(o) }

Source§

type Target = VecDeque<Object>

The resulting type after dereferencing.
Source§

impl Display for List

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl Extend<&str> for List

Source§

fn extend(&mut self, _str: &str) -> &mut Self

python list extend method Read more
Source§

impl Extend<Int<i32>> for List

Source§

fn extend(&mut self, _int: Int<i32>) -> &mut Self

python list extend method Read more
Source§

impl Extend<List> for List

Source§

fn extend(&mut self, _container: List) -> &mut Self

python list extend method Read more
Source§

impl Extend<String> for List

Source§

fn extend(&mut self, _string: String) -> &mut Self

python list extend method Read more
Source§

impl<T> Extend<Vec<T>> for List
where T: Sized, List: Append<T>,

Source§

fn extend(&mut self, _vec: Vec<T>) -> &mut Self

python list extend method Read more
Source§

impl Extend<i32> for List

Source§

fn extend(&mut self, _int: i32) -> &mut Self

python list extend method Read more
Source§

impl From<&List> for List

Source§

fn from(_list: &List) -> List

creates a list from another list; like copy constructor

Source§

impl From<&String> for List

creates a list from string example let list = List::from(“q23123123”.to_string()) or let list = List::from(String::from(“q23123123”))

Source§

fn from(_string: &String) -> List

Converts to this type from the input type.
Source§

impl From<&str> for List

Source§

fn from(_static_string: &str) -> List

Converts to this type from the input type.
Source§

impl From<String> for List

Source§

fn from(_string: String) -> List

Converts to this type from the input type.
Source§

impl From<i32> for List

Source§

fn from(_integer: i32) -> List

§Showcase - python list
// the crate name is 'python-objects'
// because there is another crate out there with `python` name
// but the lib.rs (library crate of this crate) its called `python`
// so you can import like this
extern crate python;
// actually 'extern crate' is useless
// just use only 'use python::'

// use everything from python
use python::*;

fn main() {
    // create a new python list
    let mut python_list =
        List::from(String::from("123123"));
    // at this point the list will look like this
    // ['1', '2', '3', '1', '2', '3']

    // append an integer
    python_list.append_back(123);
    // append a rust static string
    python_list.append_front("hello");
    python_list.append_back(123);
    // append a list
    python_list.append_back(List::from(String::from("working")));
    // note that the python list supports another python list inside

    // append a float
    python_list.append_back(123.123);
    python_list.append_back(123.123);
    python_list.append_back(123.123);
    // append a rust String
    python_list.append_back(String::from("asdasd"));

    python_list.append_back(
        List::from("something".to_string()));

    // append a python string
    // note that this _String is from this crate
    // its the struct that handles the String and &str data types
    python_list.append_back(
        _String::from(
            String::from("python string")));

    // append a python bool
    // note that Bool is the python struct that handles rust's bool
    python_list.append_back(Bool::new(true));
    python_list.append_back(Bool::new(false));
    // append a rust bool
    python_list.append_back(false);

    // print just like in python
    print(&python_list);

    // use len just like in python
    print(len(&python_list));


    // python_list.append_front("salutare");

    // iterate over the list just like in python
    // there are plans for future to remove the .iter()
    // so you can use for o in python_list { ... }, just that simple
    for o in python_list.iter() {
        print(o)
    }

    // create a python from parsing a static string
    let list_from_str = "123123".parse::<List>().unwrap();
    print(&list_from_str);


    let iter = (0..5).into_iter();
    // let list_from_iterator: List = iter.collect();

    // create a python list from rust iterator
    let list_from_iterator = iter.collect::<List>();
    print(&list_from_iterator);
}

output

# the list
['hello', '1', '2', '3', '1', '2', '3', 123, 123, ['w', 'o', 'r', 'k', 'i', 'n', 'g'], 123.123, 123.123, 123.123, 'asdasd', ['s', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g'], 'python string', True, False, False]
# the length
19
hello
1
2
3
1
2
3
123
123
['w', 'o', 'r', 'k', 'i', 'n', 'g']
123.123
123.123
123.123
asdasd
['s', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g']
python string
True
False
False
['1', '2', '3', '1', '2', '3']
[0, 1, 2, 3, 4]

as you can see the list contains char, float (f32), integer (i32), a rust string, another python list, a python string and many more data types.

this is just bare bones and experimental, more features will come soon. stay still!

Source§

impl FromIterator<String> for List

Source§

fn from_iter<T: IntoIterator<Item = String>>(_string_iterator: T) -> Self

Creates a value from an iterator. Read more
Source§

impl FromIterator<i32> for List

Source§

fn from_iter<T: IntoIterator<Item = i32>>(_integer_iterator: T) -> Self

Creates a value from an iterator. Read more
Source§

impl FromStr for List

Source§

type Err = Box<dyn Error>

The associated error which can be returned from parsing.
Source§

fn from_str(_static_str: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Iterable for List

if you want to use max(&list) you need the impl for &List how comes that for Object work by default

Source§

fn __len__(&self) -> usize

return the length of the collection/container/iterable
Source§

impl _Object for List

Source§

fn __repr__(&self) -> String

rules for repr daca nu ai string or char -> ‘[]’ (single quotes outsiode) daca ai string or char -> “[]” (double quotes outside)

Source§

fn __str__(&self) -> String

str documentation in List

Auto Trait Implementations§

§

impl Freeze for List

§

impl RefUnwindSafe for List

§

impl Send for List

§

impl Sync for List

§

impl Unpin for List

§

impl UnwindSafe for List

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.