Is a &reference, so the data it refers to cannot be borrowed

Hi, I've stumbled upon a problem I cannot seem to solve and I was wondering if any of you guys could help me with it. I have a structure which looks like:

#[derive(PartialEq, Eq)]
pub struct DFAState<'a> {
    pub name: Vec<&'a State>,
    pub link: HashMap<String, char>,
    pub end_state: bool,

and another one, which packs a vector of DFAState's.

pub struct DFAList<'a> {
    pub states: Vec<DFAState<'a>>,
    pub terminals: Vec<char>,

impl<'a> DFAList<'a> {
    pub fn check_connections(&mut self, state_list: &'a StateList) {
        for stmnt in self.states.iter() {
            let mut vec: Vec<&'a State> = Vec::new();
            for i in &state_list.values {
                let mut string = String::new();
                let ch = i;
                for st in {
                    for (key, val) in &st.directions {
                        if *val == *ch {
                            let str_val: Vec<char> = key.chars().collect();
                            let node: &State = &state_list.list[state_list.get_state_by_name(str_val[0]).unwrap()];
                            if !vec.contains(&node) {
                if string != "".to_string() {
          , *ch);
        println!("q{} {:?} {}", stmnt,, vec.len());

To my understanding, I'm trying to mutate a list which I'm already referencing and I don't know how to solve this. My whole project, if anyone needs it:

It doesn't quite seem like the pasted code is complete, e.g. there's an dfa_states_vec which isn't created anywhere. Additionally I'm a bit confused regarding the linked project, which files are relevant and so on? Finally it would be really nice if you could point out what the error message is, and where it goes wrong.

To answer the question in your title, a &mut reference must be unique (i.e. no other references, & or &mut), so you can indeed not modify something while you only have shared access.

When compiling the code, it fails here:

if string != "".to_string() {
    //, *ch);
    let mut arc = Arc::new(stmnt);
    Arc::make_mut(&mut arc).link.insert(string, *ch);

I don't really understand what you're trying to do. You create an Arc, then use make_mut, then throw away the Arc?? Additionally be aware that make_mut might not operate on the original value, but instead on a clone.

Note also that you should prefer &string != "" to using to_string.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.