Ok, guys, I've got a good one for you here (I think).
I've got these structs:
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Clone)]
pub struct PositionKey {
pub symbol: String,
pub group: String,
pub fund: String,
pub broker: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Position {
pub key: PositionKey,
pub country: String,
pub qty: f64,
}
and this fn:
fn mvBySymbol(positions: Vec<Position>, prices: HashMap<String, f64>) -> Vec<f64> {
let mut result = Vec::new();
for position in positions {
let price = *prices.get(&position.key.symbol).unwrap();
result.push(calcMV(position, price));
}
result
}
What I'd like to do is generalize this function so I can calc MV by symbol, group, fund, or broker - any of the PositionKey fields.
So rather than the fn accessing position.key.symbol (or whatever) directly, it'll be passed a lambda of some sort that will extract the field.
What the best/most performant/most idiomatic way to do this in Rust?