I have a function that parses an input string:
fn parse_input(s: &str) -> ((usize, usize), BTreeMap<(usize, usize), Tile>){
let mut start: (usize, usize) = (0,0);
let grid = s.split("n").enumerate().flat_map(|(r,l)| {
l.chars().enumerate().map(move |(col, c)| {
let t = classify_tile(c);
match t {
Tile::Start => {
*start = (r, col);
((r,col), t)
},
_ => ((r,col), t)
}
})
}).collect::<BTreeMap<(usize, usize), Tile>>();
(start, grid)
}
I basically want to capture the r and col value of the start tile (which is unique, only one occurrence). But currently if I try and modify the tuple inside the iterator I assume due to borrowing and scope reasons, the value doesn’t get modified outside the iterator.
It is important the iterator finishes though.
An alternative solution would be to search for the start tile in the btreemap afterwards but I’m hoping for a more efficient solution.
Should I just do this as nested for loops? Is the iteration actually meaningfully more efficient here?