Struct List

Source
pub struct List<T> { /* private fields */ }
Expand description

Lists are the KDB equivalent of Rust’s Vec. They contain collections of values and their contents be looked up by index.

§Examples

use kdb::{KBox, List, list};

let mut l = list![i32; 1, 2, 3, 4, 5];
l.push(6);
let sl = &l[..]; // we can take slices and use them like other rust slices.
assert_eq!(21, sl.iter().copied().sum());

§Appending to lists

When you append to lists in KDB, it will potentially reallocate the raw list which gives you a new and different pointer. So you can only extend lists safely if you own them, which means methods like push and join are only available on types of KBox<List<_>>.

You Notes for best performance: using list! or .collect() to create a populated list will typically result in better performance than using new and push. This is because they will, where possible, allocate a list large enough for all items up front. push will reallocate whenever needed.

Implementations§

Source§

impl<T: KListable> List<T>

Source

pub fn as_slice(&self) -> &[T::ListItem]

Returns the contents of the list as a slice

Source

pub fn as_slice_mut(&mut self) -> &mut [T::ListItem]

Returns the contents of the list as a mutable slice

Source

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

Returns an iterator over the list.

Examples found in repository?
examples/creating_a_list.rs (line 20)
3fn main() {
4    //Create a list containing the numbers from 1 to 10.
5    let mut list: KBox<List<u8>> = (1..=10).collect();
6
7    //Create another list by pushing incrementally
8    let mut list_2: KBox<List<u8>> = KBox::new_list();
9    for i in 11..=20 {
10        list_2.push(i);
11    }
12
13    //Append the second list to the first
14    list.join(list_2);
15
16    // Append from an iterator:
17    list.extend(21..=30);
18
19    // write out the contents
20    for i in list.iter() {
21        println!("{}", i);
22    }
23
24    // we can also use it as a slice:
25    for i in &list[..5] {
26        println!("{}", i)
27    }
28}
Source

pub fn len(&self) -> usize

Returns the number of elements in the list.

Source

pub fn is_empty(&self) -> bool

Returns true if the list has a length of 0.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T::ListItem>

Returns an iterator that allows modifying each value.

Source

pub fn get<I: SliceIndex<[T::ListItem]>>(&self, index: I) -> Option<&I::Output>

Returns a reference to an element or a subslice, depending on the type of index.

Source

pub fn get_mut<I: SliceIndex<[T::ListItem]>>( &mut self, index: I, ) -> Option<&mut I::Output>

Returns a mutable reference to an element or a subslice, depending on the type of index.

Source§

impl List<i8>

Source

pub fn try_as_str(&self) -> Result<&str, ConversionError>

Attempts to convert to a valid utf-8 string. This will return an error if the string contains invalid utf-8 characters. This function does not allocate.

Source

pub unsafe fn as_str_unchecked(&self) -> &str

Converts the symbol to a rust str without checking if it is valid.

§Safety

The string must be valid UTF-8. It’s length must be less than or equal to isize::MAX.

Trait Implementations§

Source§

impl<T: KListable> AsRef<Any> for List<T>

Source§

fn as_ref(&self) -> &Any

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: KListable> Index<Range<usize>> for List<T>

Source§

type Output = [<T as KListable>::ListItem]

The returned type after indexing.
Source§

fn index(&self, i: Range<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: KListable> Index<RangeFrom<usize>> for List<T>

Source§

type Output = [<T as KListable>::ListItem]

The returned type after indexing.
Source§

fn index(&self, i: RangeFrom<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: KListable> Index<RangeFull> for List<T>

Source§

type Output = [<T as KListable>::ListItem]

The returned type after indexing.
Source§

fn index(&self, _: RangeFull) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: KListable> Index<RangeTo<usize>> for List<T>

Source§

type Output = [<T as KListable>::ListItem]

The returned type after indexing.
Source§

fn index(&self, i: RangeTo<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: KListable> Index<usize> for List<T>

Source§

type Output = <T as KListable>::ListItem

The returned type after indexing.
Source§

fn index(&self, i: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: KListable> IndexMut<Range<usize>> for List<T>

Source§

fn index_mut(&mut self, i: Range<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T: KListable> IndexMut<RangeFrom<usize>> for List<T>

Source§

fn index_mut(&mut self, i: RangeFrom<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T: KListable> IndexMut<RangeFull> for List<T>

Source§

fn index_mut(&mut self, _: RangeFull) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T: KListable> IndexMut<RangeTo<usize>> for List<T>

Source§

fn index_mut(&mut self, i: RangeTo<usize>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T: KListable> IndexMut<usize> for List<T>

Source§

fn index_mut(&mut self, i: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for List<T>

§

impl<T> RefUnwindSafe for List<T>
where T: RefUnwindSafe,

§

impl<T> !Send for List<T>

§

impl<T> !Sync for List<T>

§

impl<T> Unpin for List<T>
where T: Unpin,

§

impl<T> UnwindSafe for List<T>
where T: UnwindSafe,

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<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.