0
votes

I have a similar issue as this one How can I change fields of elements in vectors in Rust?. Where I can't just seem to get it working. I have been adding '&' and 'mut' all over the place, but I am out of a clue..

#[derive(Debug)]
struct Character{
    name: String,
}

struct Characters {
    records : Vec<Character>
}

impl Characters {
    fn set_value(self, value : String) {
        for record in self.records {
            record.name = value;
        }
    }
}

fn test() {
    let  hobbits = vec![
        Character{name:String::from("Sam")},
        Character{name:String::from("Merry")},
        Character{name:String::from("Pepper")},
    ];

    let chars = Characters {
        records : hobbits
    };

    // Set all names to "Halfling 
    chars.set_value("Halfling".to_string());
}

This does not compile for several reasons, but my main thing is; I want to modify a variable of a struct inside another struct vector variable impl. Trying all sorts of things but am unable to get it to work.