Reputation: 1909
Apologies, since this seems quite a popular question. I went through a bunch of examples.
get_mut(&0).unwrap()
is not available for the self.head
object.match
statement.Hence my doubt:
use std::collections::HashMap;
#[derive(Debug)]
struct Node {
word: String,
next: Option<HashMap<char, Node>>,
}
impl Node {
fn new(word: String) -> Self {
Node {
word,
next: None,
}
}
}
#[derive(Debug)]
struct WordDictionary {
head: Option<Node>,
}
impl WordDictionary {
fn new() -> Self {
WordDictionary {
head: None,
}
}
fn add_word(&mut self, word: String) {
match &self.head { // Issue here
None => { /* Do something */ }
Some(nd) => {
self.iterate(nd, word, 0); // And here
}
}
}
fn iterate(&mut self, _parent_node: &Node, _word: String, _idx: usize) {
// Do something
}
}
#[allow(non_snake_case)]
fn main() {
{
let mut wordDictionary = WordDictionary::new();
wordDictionary.add_word("bad".to_string());
println!("{:?}", wordDictionary);
}
}
Gives the following error:
⣿
Execution
Close
Standard Error
Compiling playground v0.0.1 (/playground)
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable
--> src/main.rs:34:17
|
31 | match &self.head {
| ---------- immutable borrow occurs here
...
34 | self.iterate(nd, word, 0);
| ^^^^^-------^^^^^^^^^^^^^
| | |
| | immutable borrow later used by call
| mutable borrow occurs here
For more information about this error, try `rustc --explain E0502`.
error: could not compile `playground` due to previous error
It would be great if someone can provide insight into this specific example or help point another answered example.
Thank you!
Upvotes: 2
Views: 2573
Reputation: 344
First of all, I'm not sure that your iterate
function signature is possible. Explanation: your function takes one mutable and one immutable reference at the same time. In Rust, this is not possible. You can have only one mutable or many immutable references.
Because the iteration
function has &mut self
I suppose that you want to modify smth during the execution. To do that, only one &mut Node
will be enough. Like here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c962831b569768f05599b99cdff597bd
Upvotes: 0