Collections 迭代元组集合中已排序的元素

Collections 迭代元组集合中已排序的元素,collections,rust,iteration,Collections,Rust,Iteration,我试图在集合中以2个或更多元组的形式迭代排序元素 如果我有一个Vec,我可以打电话 for window in my_vec.windows(2) { // do something with window } 但是Vecs不是隐式排序的,拥有它真是太好了。我尝试使用b树集而不是Vec,但似乎无法在其上调用windows 打电话的时候 for window in tree_set.iter().windows(2) { // do something with window }

我试图在集合中以2个或更多元组的形式迭代排序元素

如果我有一个
Vec
,我可以打电话

for window in my_vec.windows(2) {
    // do something with window
}
但是
Vec
s不是隐式排序的,拥有它真是太好了。我尝试使用
b树集
而不是
Vec
,但似乎无法在其上调用
windows

打电话的时候

for window in tree_set.iter().windows(2) {
    // do something with window
}
我得到了错误

找不到类型为`std::collections::btree_set::Iter的名为`windows`的方法Itertools提供了以下方法:

外部板条箱工具;
使用itertools::itertools;
使用std::collections::BTreeSet;
fn main(){
让项目:BTreeSet=vec![1,3,2]。放入iter().collect();
对于items.iter().tuple_windows()中的(a,b){
println!(“{}<{}”,a,b);
}
}

请注意,
windows
是片上的方法,而不是迭代器上的方法,它返回原始片的子片的迭代器。
BTreeMap
可能无法提供相同的迭代器接口,因为它不是建立在连续大块数据之上的;在内存中,会有一些值不是紧接着后续值的

extern crate itertools;

use itertools::Itertools;
use std::collections::BTreeSet;

fn main() {
    let items: BTreeSet<_> = vec![1, 3, 2].into_iter().collect();

    for (a, b) in items.iter().tuple_windows() {
        println!("{} < {}", a, b);
    }
}