我正在尝试在我自己的Itertools类库中重命名std::iter::Iterator::scan
函数。
// A renaming of https://doc.rust-lang.org/src/core/iter/traits/iterator.rs.html#1420
fn scan_while<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
where
Self: Sized,
F: FnMut(&mut St, Self::Item) -> Option<B>,
{
Scan::new(self, initial_state, f)
}
但是,我得到错误:
// 42 | Scan::new(self, initial_state, f)
// | ^^^ private associated function
做这样的事情的习惯Rust方法是什么?
(请不要说"不要重命名")
正如caTS提到的,解决方案是:
fn scan_while<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
where
Self: Sized,
F: FnMut(&mut St, Self::Item) -> Option<B>,
{
self.scan(initial_state, f)
}