- I understand why this code does not work:
pub struct Foo {}
pub struct Bar {}
pub struct Thing {
foo: Foo,
bar: Bar
}
pub fn mov_and_combine(f: Vec<Foo>, b: Vec<Bar>) -> Vec<Thing> {
assert_eq!(f.len(), b.len());
let n = f.len();
let ans = Vec::new();
for i in 0..n {
ans.push(Thing { foo: f[i], bar: b[i] })
}
ans
}
the problem is that I am asking Rustc to move values out of the vector, but I'm passing them by referenc3e.
-
There are a number of ways to resolve this -- but none of them look elegant.
-
Is there an idiomatic way to solve this problem? A vec of Foo and a vec of Bar goes in, a vec of Thing comes out, combining the two.
Thanks!