【发布时间】:2020-06-07 23:36:21
【问题描述】:
我正在使用 Rust Itertools MultiPeek。如何有效或方便地将 next() 迭代器推进到 peek() 迭代器的当前位置?
fn main() {
let v = "abcd";
let mut mp = itertools::multipeek(v.char_indices());
if let Some((byte_offset, c)) = mp.peek() {
println!("peek: offset {}, char {}", byte_offset, c);
}
if let Some((byte_offset, c)) = mp.peek() {
println!("peek: offset {}, char {}", byte_offset, c);
}
// Update next to current location of peek assuming
// we'd rather not keep track the number of peeks
if let Some((byte_offset, c)) = mp.next() {
// would like to have Offset 2, char c
println!("next: offset {}, char {}", byte_offset, c);
}
}
游乐场link
【问题讨论】:
-
只需
mp.skip(2);或mp.nth(2) -
最小的例子可能过于简化了。我宁愿不跟踪查看了多少个字符,并且 skip() 和 nth() 导致 O(n) 遍历我已经查看过的 UTF-8 字符。
-
也许可以做一个更好的例子,同样对于精确大小的迭代器应该是 O(1)
-
试图澄清这个例子。
标签: rust