I have seen this issue/topic is covered many times in forum but the context contains match
in my case. First, let me present the method:
fn invoke(&mut self, line: &str) -> ShellResult<()> {
match line.find(' ') {
Some(index) => {
let name = &line[0..index];
let args = &line[index + 1..];
match self.codes.iter().find(|c| c.name == name) {
Some(code) => {
code.invokable.invoke(Box::new(self), args);
Ok(())
}
None => Err(ShellError::DoesNotExist {
name: name.to_owned(),
}),
}
}
None => match self.codes.iter().find(|c| c.name == line) {
Some(code) => {
code.invokable.invoke(Box::new(self), "");
Ok(())
}
None => Err(ShellError::DoesNotExist {
name: line.to_owned(),
}),
},
}
}
It fails saying:
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/lib.rs:121:56
|
119 | match self.codes.iter().find(|c| c.name == name) {
| ---------- immutable borrow occurs here
120 | Some(code) => {
121 | code.invokable.invoke(Box::new(self), args);
| ------ ^^^^ mutable borrow occurs here
| |
| immutable borrow later used by call
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/lib.rs:131:52
|
129 | None => match self.codes.iter().find(|c| c.name == line) {
| ---------- immutable borrow occurs here
130 | Some(code) => {
131 | code.invokable.invoke(Box::new(self), "");
| ------ ^^^^ mutable borrow occurs here
| |
| immutable borrow later used by call
So, as far as I see, it seems like Rust immutably borrows self
for match
ing. I need to provide self
to invoke
method, the signature of which is:
fn invoke(&self, writable: Box<&mut dyn Write>, args: &str);
And I provide self
as Box
ed because I provide it as std::io::Write
and it definitely implements that.
So, how do I provide self
to invoke
method in this case?
Environment
- Rust 1.43.1