I have the following function:
pub fn generate_mock_balances(interval: &Interval, n: &i32) -> Vec<HistoricBalance> {
let time = Utc::now();
let ticks = (1..*n).map(|v| v as i64);
let mut mocks = vec![];
for tick in ticks {
let historic_time = match interval {
Interval::Hour => time - Duration::minutes(tick),
Interval::Day => time - Duration::hours(tick),
Interval::Week => time - Duration::days(tick),
Interval::Month => time - Duration::weeks(tick),
Interval::Year => time - Duration::weeks(4 * tick),
};
mocks.push(HistoricBalance {
time: historic_time.timestamp().to_string(),
balance: (1499 * tick).to_string(),
});
}
mocks
}
I feel like the switch case within the loop is not efficient and that there must be a way to select the appropriate Duration function once and store it for use in the loop, instead of doing the check at every iteration of the loop.
I've taken a look at the documentation for closures and while I instinctively feel it's the way to go it seems more suited towards speeding up slow calculations by storing the outcome of said calculation for re-use. How do I do this?