pub struct Collection<T> { /* private fields */ }Expand description
Core collection type that wraps a vector of items.
This is the main type used throughout Lodash-RS for collection operations.
It provides a type-safe wrapper around Vec<T> with additional functionality.
Implementations§
Source§impl<T> Collection<T>
Collection methods that work on the Collection type.
impl<T> Collection<T>
Collection methods that work on the Collection type.
Sourcepub fn each<F>(&self, iteratee: F)
pub fn each<F>(&self, iteratee: F)
Iterate over elements of the collection, executing a function for each element.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
let mut sum = 0;
collection.each(|x| sum += x);
assert_eq!(sum, 6);Sourcepub fn map<U, F>(&self, iteratee: F) -> Vec<U>
pub fn map<U, F>(&self, iteratee: F) -> Vec<U>
Create an array of values by running each element through iteratee.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
let doubled = collection.map(|x| x * 2);
assert_eq!(doubled, vec![2, 4, 6]);Sourcepub fn filter<F>(&self, predicate: F) -> Vec<T>
pub fn filter<F>(&self, predicate: F) -> Vec<T>
Iterate over elements, returning an array of all elements the predicate returns truthy for.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
let evens = collection.filter(|x| x % 2 == 0);
assert_eq!(evens, vec![2, 4]);Sourcepub fn reduce<U, F>(&self, iteratee: F, initial: U) -> U
pub fn reduce<U, F>(&self, iteratee: F, initial: U) -> U
Reduce the collection to a value which is the accumulated result of running each element through iteratee.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4]);
let sum = collection.reduce(|acc, x| acc + x, 0);
assert_eq!(sum, 10);Sourcepub fn reduce_right<U, F>(&self, iteratee: F, initial: U) -> U
pub fn reduce_right<U, F>(&self, iteratee: F, initial: U) -> U
This method is like reduce except that it iterates from right to left.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec!["a", "b", "c"]);
let result = collection.reduce_right(|acc, x| format!("{}{}", acc, x), String::new());
assert_eq!(result, "cba");Sourcepub fn for_each_right<F>(&self, iteratee: F)
pub fn for_each_right<F>(&self, iteratee: F)
This method is like each except that it iterates from right to left.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
let mut result = Vec::new();
collection.for_each_right(|x| result.push(*x));
assert_eq!(result, vec![3, 2, 1]);Source§impl<T> Collection<T>
Collection methods that work on the Collection type.
impl<T> Collection<T>
Collection methods that work on the Collection type.
Sourcepub fn find<F>(&self, predicate: F) -> Option<&T>
pub fn find<F>(&self, predicate: F) -> Option<&T>
Iterate over elements, returning the first element the predicate returns truthy for.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
let first_even = collection.find(|x| x % 2 == 0);
assert_eq!(first_even, Some(&2));Sourcepub fn find_last<F>(&self, predicate: F) -> Option<&T>
pub fn find_last<F>(&self, predicate: F) -> Option<&T>
This method is like find except that it iterates from right to left.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
let last_even = collection.find_last(|x| x % 2 == 0);
assert_eq!(last_even, Some(&4));Sourcepub fn includes(&self, value: &T) -> boolwhere
T: PartialEq,
pub fn includes(&self, value: &T) -> boolwhere
T: PartialEq,
Check if value is in the collection.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
assert!(collection.includes(&3));
assert!(!collection.includes(&6));Sourcepub fn every<F>(&self, predicate: F) -> bool
pub fn every<F>(&self, predicate: F) -> bool
Check if predicate returns truthy for all elements.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![2, 4, 6, 8]);
assert!(collection.every(|x| x % 2 == 0));Sourcepub fn some<F>(&self, predicate: F) -> bool
pub fn some<F>(&self, predicate: F) -> bool
Check if predicate returns truthy for any element.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
assert!(collection.some(|x| x % 2 == 0));Sourcepub fn count_by<K, F>(&self, iteratee: F) -> HashMap<K, usize>
pub fn count_by<K, F>(&self, iteratee: F) -> HashMap<K, usize>
Create an object composed of keys generated from the results of running each element through iteratee.
§Examples
use rust_lodash::collection::Collection;
use std::collections::HashMap;
let collection = Collection::new(vec![6.1, 4.2, 6.3]);
let counts = collection.count_by(|x| (*x as f64).floor() as i32);
assert_eq!(counts.get(&6), Some(&2));Sourcepub fn partition<F>(&self, predicate: F) -> (Vec<T>, Vec<T>)
pub fn partition<F>(&self, predicate: F) -> (Vec<T>, Vec<T>)
Create an array of elements split into two groups.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
let (evens, odds) = collection.partition(|x| x % 2 == 0);
assert_eq!(evens, vec![2, 4]);
assert_eq!(odds, vec![1, 3, 5]);Source§impl<T> Collection<T>
Collection methods that work on the Collection type.
impl<T> Collection<T>
Collection methods that work on the Collection type.
Sourcepub fn group_by<K, F>(&self, iteratee: F) -> HashMap<K, Vec<T>>
pub fn group_by<K, F>(&self, iteratee: F) -> HashMap<K, Vec<T>>
Create an object composed of keys generated from the results of running each element through iteratee.
§Examples
use rust_lodash::collection::Collection;
use std::collections::HashMap;
let collection = Collection::new(vec![6.1, 4.2, 6.3]);
let grouped = collection.group_by(|x| (*x as f64).floor() as i32);
assert_eq!(grouped.get(&6), Some(&vec![6.1, 6.3]));Sourcepub fn key_by<K, F>(&self, iteratee: F) -> HashMap<K, T>
pub fn key_by<K, F>(&self, iteratee: F) -> HashMap<K, T>
Create an object composed of keys generated from the results of running each element through iteratee.
§Examples
use rust_lodash::collection::Collection;
use std::collections::HashMap;
let collection = Collection::new(vec![
("john", 30),
("jane", 25),
("bob", 35),
]);
let keyed = collection.key_by(|(name, _)| name.to_string());
assert_eq!(keyed.get("john"), Some(&("john", 30)));Sourcepub fn invoke<U, F>(&self, method: F) -> Vec<U>
pub fn invoke<U, F>(&self, method: F) -> Vec<U>
Invoke the method at path of each element.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec!["hello", "world"]);
let uppercased = collection.invoke(|s| s.to_uppercase());
assert_eq!(uppercased, vec!["HELLO", "WORLD"]);Sourcepub fn sort_by<K, F>(&self, iteratee: F) -> Vec<T>
pub fn sort_by<K, F>(&self, iteratee: F) -> Vec<T>
Create an array of elements, sorted in ascending order by the results of running each element through iteratee.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![
("john", 30),
("jane", 25),
("bob", 35),
]);
let sorted = collection.sort_by(|(_, age)| *age);
assert_eq!(sorted[0], ("jane", 25));Sourcepub fn order_by<K, F>(&self, iteratee: F, ascending: bool) -> Vec<T>
pub fn order_by<K, F>(&self, iteratee: F, ascending: bool) -> Vec<T>
This method is like sort_by except that it allows specifying the sort
orders of the iteratees to sort by.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![
("john", 30, "engineer"),
("jane", 25, "designer"),
("bob", 30, "manager"),
]);
let sorted = collection.order_by(|(_, age, _)| *age, false);
assert_eq!(sorted[0], ("john", 30, "engineer"));Source§impl<T> Collection<T>
Collection methods that work on the Collection type.
impl<T> Collection<T>
Collection methods that work on the Collection type.
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Get the size of the collection.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
assert_eq!(collection.size(), 5);Sourcepub fn shuffle(&self) -> Vec<T>where
T: Clone,
pub fn shuffle(&self) -> Vec<T>where
T: Clone,
Creates an array of shuffled values.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
let shuffled = collection.shuffle();
assert_eq!(shuffled.len(), 5);Sourcepub fn sample(&self) -> Option<&T>
pub fn sample(&self) -> Option<&T>
Gets a random element from the collection.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
let random = collection.sample();
assert!(collection.data().contains(random.unwrap()));Sourcepub fn sample_size(&self, n: usize) -> Vec<T>where
T: Clone,
pub fn sample_size(&self, n: usize) -> Vec<T>where
T: Clone,
Gets n random elements at unique keys from the collection.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3, 4, 5]);
let samples = collection.sample_size(3);
assert_eq!(samples.len(), 3);Source§impl<T> Collection<T>
impl<T> Collection<T>
Sourcepub fn new(data: impl IntoIterator<Item = T>) -> Self
pub fn new(data: impl IntoIterator<Item = T>) -> Self
Create a new collection from an iterator.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.len(), 3);Sourcepub fn empty() -> Self
pub fn empty() -> Self
Create a new empty collection.
§Examples
use rust_lodash::collection::Collection;
let collection: Collection<i32> = Collection::empty();
assert!(collection.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the length of the collection.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the collection is empty.
§Examples
use rust_lodash::collection::Collection;
let collection: Collection<i32> = Collection::empty();
assert!(collection.is_empty());
let collection = Collection::new(vec![1, 2, 3]);
assert!(!collection.is_empty());Sourcepub fn data(&self) -> &Vec<T>
pub fn data(&self) -> &Vec<T>
Get a reference to the underlying data.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.data(), &vec![1, 2, 3]);Sourcepub fn data_mut(&mut self) -> &mut Vec<T>
pub fn data_mut(&mut self) -> &mut Vec<T>
Get a mutable reference to the underlying data.
§Examples
use rust_lodash::collection::Collection;
let mut collection = Collection::new(vec![1, 2, 3]);
collection.data_mut().push(4);
assert_eq!(collection.data(), &vec![1, 2, 3, 4]);Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Get an item at the specified index.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.get(1), Some(&2));
assert_eq!(collection.get(5), None);Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Get a mutable item at the specified index.
§Examples
use rust_lodash::collection::Collection;
let mut collection = Collection::new(vec![1, 2, 3]);
if let Some(item) = collection.get_mut(1) {
*item = 42;
}
assert_eq!(collection.get(1), Some(&42));Sourcepub fn first(&self) -> Option<&T>
pub fn first(&self) -> Option<&T>
Get the first item in the collection.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.first(), Some(&1));
let empty: Collection<i32> = Collection::empty();
assert_eq!(empty.first(), None);Sourcepub fn last(&self) -> Option<&T>
pub fn last(&self) -> Option<&T>
Get the last item in the collection.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.last(), Some(&3));
let empty: Collection<i32> = Collection::empty();
assert_eq!(empty.last(), None);Sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Convert the collection to a vector.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
let vec = collection.into_vec();
assert_eq!(vec, vec![1, 2, 3]);Sourcepub fn into_iter(self) -> IntoIter<T>
pub fn into_iter(self) -> IntoIter<T>
Convert the collection to an iterator.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
let sum: i32 = collection.into_iter().sum();
assert_eq!(sum, 6);Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Get an iterator over references to the items.
§Examples
use rust_lodash::collection::Collection;
let collection = Collection::new(vec![1, 2, 3]);
let sum: i32 = collection.iter().sum();
assert_eq!(sum, 6);Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Get a mutable iterator over references to the items.
§Examples
use rust_lodash::collection::Collection;
let mut collection = Collection::new(vec![1, 2, 3]);
for item in collection.iter_mut() {
*item *= 2;
}
assert_eq!(collection.data(), &vec![2, 4, 6]);Trait Implementations§
Source§impl<T: Clone> Clone for Collection<T>
impl<T: Clone> Clone for Collection<T>
Source§fn clone(&self) -> Collection<T>
fn clone(&self) -> Collection<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more