I am faced with implementing some algorithm (I have no way of changing it if I want to keep its properties) that could be summarized in the following way :
let buffer : Vec<Vec<_>> = Vec::with_capacity(some_array.len()); // Just there to gather the running results
buffer.push(first_value); // We provide a starting value for the algorithm
for some_value in some_array {
let previous_value = buffer.last().unwrap(); // We retrieve the last computed value of the algorithm (or the starting value)
let new_value = expensive_function(some_value, many, non_relevant, other, parameters);
buffer.push(new_value);
}
// We have `buffer` that contains all of our values
For some reason, I have an itch that tells me that there is a way to use iterators for that (without triggering my lovely friend the borrow checker), is true ? If yes, what is it ?
Do you have a runnable example in the playground showing the behaviour you want? buffer[buffer.len()] always panics, so this one doesn't seem like it'd ever do what you want...