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>
impl<T: KListable> List<T>
Sourcepub fn as_slice_mut(&mut self) -> &mut [T::ListItem]
pub fn as_slice_mut(&mut self) -> &mut [T::ListItem]
Returns the contents of the list as a mutable slice
Sourcepub fn iter(&self) -> Iter<'_, T::ListItem>
pub fn iter(&self) -> Iter<'_, T::ListItem>
Returns an iterator over the list.
Examples found in repository?
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}
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T::ListItem>
pub fn iter_mut(&mut self) -> IterMut<'_, T::ListItem>
Returns an iterator that allows modifying each value.
Sourcepub fn get<I: SliceIndex<[T::ListItem]>>(&self, index: I) -> Option<&I::Output>
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.
Sourcepub fn get_mut<I: SliceIndex<[T::ListItem]>>(
&mut self,
index: I,
) -> Option<&mut I::Output>
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>
impl List<i8>
Sourcepub fn try_as_str(&self) -> Result<&str, ConversionError>
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.
Sourcepub unsafe fn as_str_unchecked(&self) -> &str
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.