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
等于条件1不被满足的次数,m
等于条件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();
我知道我可以创建两个向量然后连接它们,但这样做效率较低。
您可以使用此代码理解 repeat 的含义:
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);
}