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
Methods from Deref<Target = VecDeque<Object>>
1.0.0 · sourcepub fn get(&self, index: usize) -> Option<&T>
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);
assert_eq!(buf.get(1), Some(&4));
1.0.0 · sourcepub fn capacity(&self) -> usize
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);
sourcepub fn allocator(&self) -> &A
🔬 This is a nightly-only experimental API. (allocator_api
)
pub fn allocator(&self) -> &A
allocator_api
)Returns a reference to the underlying allocator.
1.0.0 · sourcepub fn iter(&self) -> Iter<'_, T>
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 · sourcepub fn as_slices(&self) -> (&[T], &[T])
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.
Examples
use std::collections::VecDeque;
let mut deque = VecDeque::new();
deque.push_back(0);
deque.push_back(1);
deque.push_back(2);
assert_eq!(deque.as_slices(), (&[0, 1, 2][..], &[][..]));
deque.push_front(10);
deque.push_front(9);
assert_eq!(deque.as_slices(), (&[9, 10][..], &[0, 1, 2][..]));
1.0.0 · sourcepub fn len(&self) -> usize
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 · sourcepub fn is_empty(&self) -> bool
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 · sourcepub fn range<R>(&self, range: R) -> Iter<'_, T> where
R: RangeBounds<usize>,
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 · sourcepub fn contains(&self, x: &T) -> bool where
T: PartialEq<T>,
pub fn contains(&self, x: &T) -> bool where
T: PartialEq<T>,
Returns true
if the deque contains an element equal to the
given value.
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 · sourcepub fn front(&self) -> Option<&T>
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 · sourcepub fn back(&self) -> Option<&T>
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));
1.54.0 · sourcepub fn binary_search(&self, x: &T) -> Result<usize, usize> where
T: Ord,
pub fn binary_search(&self, x: &T) -> Result<usize, usize> where
T: Ord,
Binary searches the sorted deque for a given element.
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:
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.binary_search(&num).unwrap_or_else(|x| x);
deque.insert(idx, num);
assert_eq!(deque, &[0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55]);
1.54.0 · sourcepub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> where
F: FnMut(&'a T) -> Ordering,
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> where
F: FnMut(&'a T) -> Ordering,
Binary searches the sorted deque with a comparator function.
The comparator function should implement an order consistent
with the sort order of the deque, returning an order code that
indicates whether its argument is Less
, Equal
or Greater
than the desired target.
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 · sourcepub 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,
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 the sorted deque 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 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 · sourcepub fn partition_point<P>(&self, pred: P) -> usize where
P: FnMut(&T) -> bool,
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 a 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)));
Trait Implementations
sourceimpl Append<&'_ str> for List
impl Append<&'_ str> for List
sourcefn append_back(&mut self, _str: &str) -> &mut Self
fn append_back(&mut self, _str: &str) -> &mut Self
Performs append
sourceimpl Append<Bool> for List
impl Append<Bool> for List
sourcefn append_back(&mut self, _bool: Bool) -> &mut Self
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]
sourceimpl Append<Float<f32>> for List
impl Append<Float<f32>> for List
sourcefn append_back(&mut self, _float: Float<f32>) -> &mut Self
fn append_back(&mut self, _float: Float<f32>) -> &mut Self
Performs append
sourceimpl Append<Float<f64>> for List
impl Append<Float<f64>> for List
sourcefn append_back(&mut self, _float: Float<f64>) -> &mut Self
fn append_back(&mut self, _float: Float<f64>) -> &mut Self
Performs append
sourceimpl Append<Int<i32>> for List
impl Append<Int<i32>> for List
sourcefn append_back(&mut self, _integer: Int<i32>) -> &mut Self
fn append_back(&mut self, _integer: Int<i32>) -> &mut Self
Performs append
sourceimpl Append<Int<i64>> for List
impl Append<Int<i64>> for List
sourcefn append_back(&mut self, _integer: Int<i64>) -> &mut Self
fn append_back(&mut self, _integer: Int<i64>) -> &mut Self
Performs append
sourceimpl Append<List> for List
impl Append<List> for List
sourcefn append_back(&mut self, _list: List) -> &mut Self
fn append_back(&mut self, _list: List) -> &mut Self
Performs append
sourceimpl Append<Object> for List
impl Append<Object> for List
sourcefn append_back(&mut self, object: Object) -> &mut Self
fn append_back(&mut self, object: Object) -> &mut Self
Performs append
sourceimpl Append<String> for List
impl Append<String> for List
sourcefn append_back(&mut self, string: String) -> &mut Self
fn append_back(&mut self, string: String) -> &mut Self
Performs append
sourceimpl Append<_String> for List
impl Append<_String> for List
sourcefn append_back(&mut self, _string: _String) -> &mut Self
fn append_back(&mut self, _string: _String) -> &mut Self
Performs append
sourceimpl Append<bool> for List
impl Append<bool> for List
sourcefn append_back(&mut self, _bool: bool) -> &mut Self
fn append_back(&mut self, _bool: bool) -> &mut Self
Performs append
sourceimpl Append<char> for List
impl Append<char> for List
sourcefn append_back(&mut self, _char: char) -> &mut Self
fn append_back(&mut self, _char: char) -> &mut Self
Performs append
sourceimpl Append<f32> for List
impl Append<f32> for List
sourcefn append_back(&mut self, _float: f32) -> &mut Self
fn append_back(&mut self, _float: f32) -> &mut Self
Performs append
sourceimpl Append<f64> for List
impl Append<f64> for List
sourcefn append_back(&mut self, _float: f64) -> &mut Self
fn append_back(&mut self, _float: f64) -> &mut Self
Performs append
sourceimpl Append<i32> for List
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]
sourcefn append_back(&mut self, _integer: i32) -> &mut Self
fn append_back(&mut self, _integer: i32) -> &mut Self
Performs append
sourceimpl Append<i64> for List
impl Append<i64> for List
sourcefn append_back(&mut self, _integer: i64) -> &mut Self
fn append_back(&mut self, _integer: i64) -> &mut Self
Performs append
sourceimpl AppendFront<&'_ str> for List
impl AppendFront<&'_ str> for List
sourcefn append_front(&mut self, _str: &str) -> &mut Self
fn append_front(&mut self, _str: &str) -> &mut Self
performs the append front
sourceimpl AppendFront<Bool> for List
impl AppendFront<Bool> for List
sourcefn append_front(&mut self, _bool: Bool) -> &mut Self
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]
sourceimpl AppendFront<List> for List
impl AppendFront<List> for List
sourcefn append_front(&mut self, _list: List) -> &mut Self
fn append_front(&mut self, _list: List) -> &mut Self
performs the append front
sourceimpl AppendFront<String> for List
impl AppendFront<String> for List
sourcefn append_front(&mut self, string: String) -> &mut Self
fn append_front(&mut self, string: String) -> &mut Self
performs the append front
sourceimpl AppendFront<_String> for List
impl AppendFront<_String> for List
sourcefn append_front(&mut self, _string: _String) -> &mut Self
fn append_front(&mut self, _string: _String) -> &mut Self
performs the append front
sourceimpl AppendFront<bool> for List
impl AppendFront<bool> for List
sourcefn append_front(&mut self, _bool: bool) -> &mut Self
fn append_front(&mut self, _bool: bool) -> &mut Self
performs the append front
sourceimpl AppendFront<char> for List
impl AppendFront<char> for List
sourcefn append_front(&mut self, _char: char) -> &mut Self
fn append_front(&mut self, _char: char) -> &mut Self
performs the append front
sourceimpl AppendFront<f32> for List
impl AppendFront<f32> for List
sourcefn append_front(&mut self, _float: f32) -> &mut Self
fn append_front(&mut self, _float: f32) -> &mut Self
performs the append front
sourceimpl AppendFront<f64> for List
impl AppendFront<f64> for List
sourcefn append_front(&mut self, _float: f64) -> &mut Self
fn append_front(&mut self, _float: f64) -> &mut Self
performs the append front
sourceimpl AppendFront<i32> for List
impl AppendFront<i32> for List
sourcefn append_front(&mut self, _integer: i32) -> &mut Self
fn append_front(&mut self, _integer: i32) -> &mut Self
performs the append front
sourceimpl From<&'_ String> for List
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”))
sourceimpl From<i32> for List
impl From<i32> for List
sourcefn from(_integer: i32) -> List
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
['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]
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!
sourceimpl FromIterator<String> for List
impl FromIterator<String> for List
sourcefn from_iter<T: IntoIterator<Item = String>>(_string_iterator: T) -> Self
fn from_iter<T: IntoIterator<Item = String>>(_string_iterator: T) -> Self
Creates a value from an iterator. Read more
sourceimpl FromIterator<i32> for List
impl FromIterator<i32> for List
sourcefn from_iter<T: IntoIterator<Item = i32>>(_integer_iterator: T) -> Self
fn from_iter<T: IntoIterator<Item = i32>>(_integer_iterator: T) -> Self
Creates a value from an iterator. Read more
Auto Trait Implementations
impl RefUnwindSafe for List
impl Send for List
impl Sync for List
impl Unpin for List
impl UnwindSafe for List
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more