我试图用group_by_count方法扩展Iterator:
use itertools::Itertools;
trait ExtIterator<T>
where
T: Sized,
{
fn group_by_count(self) -> Box<dyn Iterator<Item = (T, usize)>>
where
T: Sized;
}
impl<T: 'static, I: 'static> ExtIterator<T> for I
where
I: Iterator<Item = T> + Sized,
T: Clone + Eq + PartialEq + Sized,
{
fn group_by_count(self) -> Box<dyn Iterator<Item = (T, usize)>>
where
Self: Sized,
{
Box::new(
self.group_by(|i| i.clone())
.into_iter()
.map(|(key, group)| (key, group.count())),
)
}
}我得到了:
error[E0515]: cannot return value referencing temporary value
--> src/ext/iterator.rs:21:9
|
21 | / Box::new(
22 | | self.group_by(|i| i.clone())
| | ---------------------------- temporary value created here
23 | | .into_iter()
24 | | .map(|(key, group)| (key, group.count())),
25 | | )
| |_________^ returns a value referencing data owned by the current function
|
= help: use `.collect()` to allocate the iterator在这里调用collect是不对的,而clippy建议如果我这样做,collect应该被删除。但我想不出如何避免创造一个暂时的价值。
发布于 2022-04-14 01:17:17
如果没有自定义迭代器适配器,就不能这样做。
但是很容易创建一个新的GroupByCount迭代器,然后根据对into_iter() on GroupBy的多次调用更改底层迭代器的事实,实现它的next()方法:
pub struct GroupByCount<I: Iterator> {
// This `fn(&K) -> K` could be `Box<dyn FnMut(&K) -> K`, but
// this adds one usize to the struct's size, and the function
// captures nothing.
inner: itertools::structs::GroupBy<I::Item, I, fn(&I::Item) -> I::Item>,
}
impl<I> Iterator for GroupByCount<I>
where
I: Iterator,
I::Item: PartialEq,
{
type Item = (I::Item, usize);
fn next(&mut self) -> Option<Self::Item> {
self.inner
.into_iter()
.next()
.map(|(key, group)| (key, group.count()))
}
}
pub trait IteratorExt: Iterator {
fn group_by_count(self) -> GroupByCount<Self>
where
Self: Sized;
}
impl<I> IteratorExt for I
where
I: Iterator,
I::Item: Clone + PartialEq,
{
fn group_by_count(self) -> GroupByCount<Self>
where
Self: Sized,
{
GroupByCount {
inner: self.group_by(|i| i.clone()),
}
}
}游乐场。
我所做的其他改进:
Sized边界。'static边界。Eq绑定,因为这是不必要的。T,然后显式地使用I::Item。ExtIterator重命名为IteratorExt,与RFC 445,扩展特性公约相同。https://stackoverflow.com/questions/71865054
复制相似问题