Module 5HashMap & BTreeMapLesson 4 of 820 min
HashMap Portfolios
Aggregate fills into net positions by ticker.
HashMap
rust
use std::collections::HashMap;
let mut port = HashMap::new();
port.insert("AAPL".to_string(), 100.0);entry API
rust
*port.entry(t).or_insert(0.0) += qty;Avoids double lookup when aggregating trades.
Challenge
Track average price per ticker from (ticker, qty, px) fills.
Tips
- entry API is idiomatic
- HashMap order is random
- String keys when owned
Try it live
HashMap PortfoliosRust 2021
Loading editor…
Output
Press Run to compile and execute.