Collection

Struct Collection 

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

Source

pub fn each<F>(&self, iteratee: F)
where F: FnMut(&T),

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);
Source

pub fn map<U, F>(&self, iteratee: F) -> Vec<U>
where F: Fn(&T) -> 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]);
Source

pub fn filter<F>(&self, predicate: F) -> Vec<T>
where T: Clone, F: Fn(&T) -> bool,

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]);
Source

pub fn reduce<U, F>(&self, iteratee: F, initial: U) -> U
where F: Fn(U, &T) -> 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);
Source

pub fn reduce_right<U, F>(&self, iteratee: F, initial: U) -> U
where F: Fn(U, &T) -> 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");
Source

pub fn for_each_right<F>(&self, iteratee: F)
where F: FnMut(&T),

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.

Source

pub fn find<F>(&self, predicate: F) -> Option<&T>
where F: Fn(&T) -> bool,

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));
Source

pub fn find_last<F>(&self, predicate: F) -> Option<&T>
where F: Fn(&T) -> bool,

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));
Source

pub fn includes(&self, value: &T) -> bool
where 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));
Source

pub fn every<F>(&self, predicate: F) -> bool
where F: Fn(&T) -> 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));
Source

pub fn some<F>(&self, predicate: F) -> bool
where F: Fn(&T) -> 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));
Source

pub fn count_by<K, F>(&self, iteratee: F) -> HashMap<K, usize>
where K: Hash + Eq, F: Fn(&T) -> K,

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));
Source

pub fn partition<F>(&self, predicate: F) -> (Vec<T>, Vec<T>)
where T: Clone, F: Fn(&T) -> bool,

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.

Source

pub fn group_by<K, F>(&self, iteratee: F) -> HashMap<K, Vec<T>>
where K: Hash + Eq, T: Clone, F: Fn(&T) -> K,

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]));
Source

pub fn key_by<K, F>(&self, iteratee: F) -> HashMap<K, T>
where K: Hash + Eq, T: Clone, F: Fn(&T) -> K,

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)));
Source

pub fn invoke<U, F>(&self, method: F) -> Vec<U>
where F: Fn(&T) -> 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"]);
Source

pub fn sort_by<K, F>(&self, iteratee: F) -> Vec<T>
where T: Clone, K: Ord, F: Fn(&T) -> K,

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));
Source

pub fn order_by<K, F>(&self, iteratee: F, ascending: bool) -> Vec<T>
where T: Clone, K: Ord, F: Fn(&T) -> K,

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.

Source

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);
Source

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);
Source

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()));
Source

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>

Source

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);
Source

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());
Source

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);
Source

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());
Source

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]);
Source

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]);
Source

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);
Source

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));
Source

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);
Source

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);
Source

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]);
Source

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);
Source

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);
Source

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>

Source§

fn clone(&self) -> Collection<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Collection<T>

Source§

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

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

impl<T> From<Collection<T>> for Vec<T>

Source§

fn from(collection: Collection<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for Collection<T>

Source§

fn from(data: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> Index<usize> for Collection<T>

Source§

type Output = T

The returned type after indexing.
Source§

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

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

impl<T> IndexMut<usize> for Collection<T>

Source§

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

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

impl<'a, T> IntoIterator for &'a Collection<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Collection<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for Collection<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for Collection<T>

Source§

fn eq(&self, other: &Collection<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for Collection<T>

Source§

impl<T> StructuralPartialEq for Collection<T>

Auto Trait Implementations§

§

impl<T> Freeze for Collection<T>

§

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

§

impl<T> Send for Collection<T>
where T: Send,

§

impl<T> Sync for Collection<T>
where T: Sync,

§

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

§

impl<T> UnwindSafe for Collection<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> SafeClone for T
where T: Clone,

Source§

fn safe_clone(&self) -> T

Safely clone the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V