let vec = iter::repeat("don't satisfy condition 1") // iterator such as next() always "don't " satisfy condition 1"
.take_while(|_| {
satisfycondition1.satisfy() // true is condition 1 is satisfied else false
})
.collect();
此代码创建一个n
元素向量,该向量n
等于不遵守条件 1 的次数。
我现在想创建一个n + m
元素向量,其n
等于不遵守条件 1m
的次数和不遵守条件 2 的次数。
代码应该类似于这样:
let vec = iter::repeat("dont't satisfy condition 1")
.take_while(|_| {
satisfycondition1.satisfy()
})
.union(
iter::repeat("has satisfed condition 1 but not 2 yet")
.take_while(|_| {
satisfycondition2.satisfy()
})
)
.collect();
我知道我可以创建两个向量,然后将它们连接起来,但效率较低。
您可以使用此代码来了解重复的内容:
use std::iter;
fn main() {
let mut c = 0;
let z: Vec<_> = iter::repeat("dont't satisfy condition 1")
.take_while(|_| {
c = c + 1;
let rep = if c < 5 { true } else { false };
rep
})
.collect();
println!("------{:?}", z);
}