Skip to main content

CollectionExt

Struct CollectionExt 

Source
pub struct CollectionExt;
Expand description

集合扩展工具

Implementations§

Source§

impl CollectionExt

Source

pub fn group_by<T, K, F>(items: &[T], key_fn: F) -> HashMap<K, Vec<&T>>
where K: Eq + Hash, F: Fn(&T) -> K,

按指定键分组(类似 SQL GROUP BY)

§示例
use wordval_utils::collection::CollectionExt;

let items = vec![
    ("apple", 1),
    ("banana", 2),
    ("apple", 3),
];
let grouped = CollectionExt::group_by(&items, |item| item.0);
assert_eq!(grouped.get("apple").unwrap().len(), 2);
Source

pub fn dedup<T>(items: Vec<T>) -> Vec<T>
where T: Eq + Hash + Clone,

去重并保持原始顺序

§示例
use wordval_utils::collection::CollectionExt;

let v = vec![3, 1, 2, 1, 3, 4];
assert_eq!(CollectionExt::dedup(v), vec![3, 1, 2, 4]);
Source

pub fn chunks<T>(items: Vec<T>, size: usize) -> Vec<Vec<T>>

将大列表切成固定大小的块

§示例
use wordval_utils::collection::CollectionExt;

let v = vec![1, 2, 3, 4, 5];
let chunks = CollectionExt::chunks(v, 2);
assert_eq!(chunks, vec![vec![1, 2], vec![3, 4], vec![5]]);
Source

pub fn find_first<T, F>(items: &[T], predicate: F) -> Option<&T>
where F: Fn(&T) -> bool,

查找第一个满足条件的元素

§示例
use wordval_utils::collection::CollectionExt;

let items = vec![1, 2, 3, 4, 5];
assert_eq!(CollectionExt::find_first(&items, |x| *x > 3), Some(&4));
Source

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

查找最后一个满足条件的元素

§示例
use wordval_utils::collection::CollectionExt;

let items = vec![1, 2, 3, 4, 5];
assert_eq!(CollectionExt::find_last(&items, |x| *x < 4), Some(&3));
Source

pub fn find_all<T, F>(items: &[T], predicate: F) -> Vec<&T>
where F: Fn(&T) -> bool,

查找所有满足条件的元素

§示例
use wordval_utils::collection::CollectionExt;

let items = vec![1, 2, 3, 4, 5];
assert_eq!(CollectionExt::find_all(&items, |x| *x % 2 == 0), vec![&2, &4]);

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more