Hi,
I am looking fo a fact way to flatten two vectors. When I say flatten I mean the following:
Let say i have two vectors:
let a = vec![b'a',b'b',b'c',b'\n',b'b',b'c',b'\n',b'k',b'c',b'k',b'\n',b'j',b'g',b'k',b'h',b'\n'];
let b = vec![b'a',b'b',b'\n',b'b',b'c',b'\n',b'k',b'k',b'k',b'\n',b'j',b'g',b'h',b'\n'];
What I aim to get is one vector of the following form:
let c = vec![b'a',b'b',b'c',b'\n' , b'a',b'b',b'\n', b'b',b'c',b'\n', b'b',b'c',b'\n', b'k',b'c',b'k',b'\n', ,b'k',b'k',b'k',b'\n', b'j',b'g',b'k',b'h',b'\n', b'j',b'g',b'h',b'\n'];
One solution would be
fn main(){
let a = vec![b'a',b'b',b'c',b'\n',b'b',b'c',b'\n',b'k',b'c',b'k',b'\n',b'j',b'g',b'k',b'h',b'\n'];
let b = vec![b'a',b'b',b'\n',b'b',b'c',b'\n',b'k',b'k',b'k',b'\n',b'j',b'g',b'h',b'\n'];
let mut c : Vec<u8> = Vec::new();
let mut j=0;
let mut k=0;
for _i in 0..4{
while a[j] != 10 {
c.push(a[j]);
j=j+1;
}
c.push(a[j]);
j=j+1;
while b[k] != 10 {
c.push(b[k]);
k=k+1;
}
c.push(b[k]);
k=k+1;
}
println!("{:?}", c);
}
But this has many push() operations. To me this means a lot of memory alocations. Is there any other more efficient solution ?
thnx