Rust 如何通过适配器在一行中重复或连接两个切片n次?
我能用iter适配器做同样的工作吗Rust 如何通过适配器在一行中重复或连接两个切片n次?,rust,Rust,我能用iter适配器做同样的工作吗 fn mutiply_bytes(buf_in: &[u8], mul: usize) -> Vec<u8> { let length = buf_in.len() * mul; let mut buf_out = Vec::with_capacity(length); for i in 0..length{ buf_out.push(buf_in[i%buf_in.len()]); }
fn mutiply_bytes(buf_in: &[u8], mul: usize) -> Vec<u8> {
let length = buf_in.len() * mul;
let mut buf_out = Vec::with_capacity(length);
for i in 0..length{
buf_out.push(buf_in[i%buf_in.len()]);
}
buf_out
}
fn多字节(buf\u in:&[u8],mul:usize)->Vec{
设长度=buf_in.len()*mul;
让mut buf_out=Vec::具有_容量(长度);
对于长度为0.的i{
buf_out.push(buf_in[i%buf_in.len()]);
}
布福
}
可能会有帮助
fn mutiply_bytes(buf_in: &[u8], mul: usize) -> Vec<u8> {
std::iter::repeat(buf_in)
.take(mul)
.flatten()
.cloned()
.collect::<Vec<u8>>()
}
fn多字节(buf\u in:&[u8],mul:usize)->Vec{
标准:国际热核聚变实验堆:重复
.拿(多个)
.flatte()
.克隆()
收集::()
}
well看起来棒极了