I know that the title is a bit strange but I couldn't think of something better.
The following code fails to compile but the second one succeeds. I think that both should be fine.
1.
fn load() -> io::Result<Vec<usize>> {
...
for dir in [<paths>....] {
store_first_err_ret_ok(&mut first_error, fs::read_dir(dir)).map(|dir| {
dir
.into_iter()
.filter_map(|entry| store_first_err_ret_ok(&mut first_error, entry))
.map(|e| File::open(e.path()))
.filter_map(|entry| store_first_err_ret_ok(&mut first_error, entry))
.for_each(|entry| res.push(entry.len()))
});
}
...
}
error:
error[E0499]: cannot borrow `first_error` as mutable more than once at a time
|
| .filter_map(|entry| store_first_err_ret_ok(&mut first_error, entry))
| ------- ----------- first borrow occurs due to use of `first_error` in closure
| |
| first mutable borrow occurs here
| .map(|e| File::open(e.path()))
| .filter_map(|entry| store_first_err_ret_ok(&mut first_error, entry))
| ---------- ^^^^^^^ ----------- second borrow occurs due to use of `first_error` in closure
| | |
| | second mutable borrow occurs here
| first borrow later used by call
2.
fn load() -> io::Result<Vec<usize>> {
...
for dir in [<paths>....] {
store_first_err_ret_ok(&mut first_error, fs::read_dir(dir)).map(|dir| {
let inter = dir
.into_iter()
.filter_map(|entry| store_first_err_ret_ok(&mut first_error, entry))
.collect();
inter
.into_iter()
.map(|e| File::open(e.path()))
.filter_map(|entry| store_first_err_ret_ok(&mut first_error, entry))
.for_each(|entry| res.push(entry.len()))
});
}
...
}
I guess my quandry