Background
Given an enum variant type (non-copyable):
enum AstNode {
Op(Func, Box<AstNode>, Box<AstNode>),
Val(f64),
// others...
}
Attempting to run an operation over two such vectors:
fn apply_func_iterative(func: Func, lhs: Vec<AstNode>, rhs: Vec<AstNode>) -> Vec<AstNode> {
lhs.iter().zip(rhs).map(|(&l,r)| apply_func(func,l,r)).collect() // l and r are declared differently!
}
fn apply_func(func: Func, lhs: AstNode, rhs: AstNode) -> AstNode {
// magic happens here!
}
Within the closure, the lhs elements assume type l: &AstNode whereas the zipped rhs elements take type l: AstNode. (Note the different declarations in the closure tuple).
Questions
Why is this the case?
Is there a way to iterate elements of a vector by value instead of by reference? It would seem this is possible from the observed behavior of the zipped elements.
(In this example, this difference results in a slight oddity in syntax declaration, but in practice, I have hit up against the borrow-checker when passing the reference variable into a function)
Disclaimer: I am fairly new to Rust