// Iterators of Code 2026 — Day 16: Account Balances // Challenge: Emit the balance after each transaction, starting from an opening // balance. All amounts are cents; negative balances are allowed. Refactor the // loop without losing intermediate balances. Every balance fits in i64. fn account_balances(opening: i64, transactions: &[i64]) -> Vec { let mut balance = opening; let mut balances = Vec::new(); for amount in transactions { balance += amount; balances.push(balance); } balances } fn main() { assert_eq!( account_balances(10_000, &[-2_500, 5_000, -1_200]), vec![7_500, 12_500, 11_300] ); assert_eq!(account_balances(100, &[-150, 100]), vec![-50, 50]); assert_eq!(account_balances(-200, &[50, 150]), vec![-150, 0]); // A zero-value transaction still produces a balance. assert_eq!(account_balances(500, &[0, 0]), vec![500, 500]); assert_eq!(account_balances(0, &[75]), vec![75]); // With no transactions, even the opening balance is not emitted. assert!(account_balances(500, &[]).is_empty()); }