Here is my struct:
pub struct Scanner<'a> {
filepath: String,
header: Option<&'a Header>,
field_counters: Option<Counters>,
}
Here is a function that is part of the implementation. It is this function that everything seems to hinge.
pub fn run(&mut self) -> Option<&'a Counters> {
if let Some(mut counters) = self.field_counters.take() {
count_field_in_file(&self.filepath, &mut counters).ok()
} else {
None
}
}
It utilizes a function that takes and returns a &mut
(effectively anyway).
pub fn count_field_in_file<'a>(
filepath: &str,
counters: &'a mut Counters,
) -> Result<&'a Counters, Box<dyn Error>> { /* go do it */}
With this latest iteration of the run
function, because I transfer ownership to the function, I then get caught with "returns a value referencing data owned by the current function". I get the error... perfect sense. What I don't get, is how to think differently about how to get the pieces of this puzzle to fit.
I believe the challenge is how to access the value both to share a &mut
to update the value... it's like a mutate in place except that I'm dealing with two different enums! I could change the struct to accomodate the situation...
Any ideas would be greatly appreciated.
- E