I am working on my side project "rg_explorer". I've written a topic on this forum introducing the project, and I have an error that has been bugging me since many days ago.
I am trying to solve the issue Optimize the wrapper to search context in only the file displayed on "Detail" view of "Nodes" tab. I need to launch ripgrep only in the selected file when I want more or less lines of context around the matches of that file, instead of running ripgrep in the whole source code I'm exploring each time I hit an arrow key.
The error is this:
error[E0502]: cannot borrow `self` as immutable because it is also borrowed as mutable
--> src/rip_grep/mod.rs:77:26
|
76 | let n: &mut Node = self.nodes.0.get_mut(i).unwrap();
| ----------------------- mutable borrow occurs here
77 | n.update_context(&self, delta);
| -------------- ^^^^^ immutable borrow occurs here
| |
| mutable borrow later used by call
For more information about this error, try `rustc --explain E0502`.
error: could not compile `rg_explorer` due to previous error
Line 76 is a function of the RipGrep
struct, it takes the nodes
field that is a Vec, gets a mutable ref to the i
element inside the vector and unwraps it.
On line 77 it calls the update_context
function from the Node
struct and calls it. It needs to send the self
argument, because RipGrep struct has a function that launches ripgrep, and I need to launch this function inside the i-node I've got on line 76. I can't figure out how to get rid of this error. I've tried another alternative solution, where I use another path to call the rip grep instance, but I end up anyways with this same error.
Am I approaching this problem the right way? How can I fix this? Why the compiler is highlighting the update_context
function? I don't see the mutable borrow being used there.
You can find the code in the branch issue_1-optimize_wrapper_change_context_only_current_file-alt1
of the repository, you can access the repo by clicking the second link, or the reply I'm going to post below (beginners are restricted to only two links, sorry).
Best regards.
PS: If you click the second link, you can see the branch I am