Newbie question. I think I understand why the code below fails. I have a function that gets a Vec of items (not a ref, the vector itself) and I'd like to do some things based on what is in the vector.
It is complaining because it returns something that references items, which is borrowed in the for loop.
How do I get "around" that?
impl TryFrom<Vec<Item>> for Ledger<'_> {
type Error = Error;
fn try_from(items: Vec<Item>) -> Result<Self, Self::Error> {
let mut ledger = Ledger::new();
// 1. Populate the lists
for item in items.iter() {
match item {
Item::Comment(_) => {}
Item::Transaction(parsed) => {
for p in parsed.postings.iter() {
let account = Account::from(p.account.clone());
ledger.accounts.push(account);
// Currencies
if let Some(c) = &p.money_currency {
ledger.currencies.push(Currency::from(c.as_str()));
}
if let Some(c) = &p.cost_currency {
let currency = Currency::from(c.as_str());
ledger.currencies.push(currency);
}
if let Some(c) = &p.balance_currency {
let currency = Currency::from(c.as_str());
ledger.currencies.push(currency);
}
}
}
Item::Directive => {}
}
}
return Ok(ledger);
}