Hi. I want to change the value of the fields in a struct. Every field is passing to a vector (in a tuple), and every tuple is passing in an iteractor. I'm messing up with the borrow system in rust, and I'm stuck.
struct Colors {
bg: u32,
fg: u32,
font: u32,
}
impl Default for Colors {
fn default() -> Self {
Colors {
bg: 0x0,
fg: 0xffffff,
font: 0xff0000,
}
}
}
//A more complex function that read from files,
//but for this sample always returns "ff00ff"
fn get_color(_color_name: String) -> String {
"ff00ff".to_string()
}
fn main() {
let mut colors = Colors::default(); // Compiler warning variable does not need to be mutable
a
let colors_tuple = vec![
("background", &colors.bg),
("foreground", &colors.fg),
("font", &colors.font),
];
for &(mut tuple) in colors_tuple.iter() {
let hex_string = get_color(tuple.0.to_string());
match u32::from_str_radix(&hex_string, 32) {
Ok(v) => tuple.1 = &v, // How can I change colors.xxx value?
Err(_e) => panic!("BAD HEX NUMBER!"),
}
}
println!("background: {}", colors.bg); //Writes 0. must be 0xff00ff
}
warning: variable does not need to be mutable
--> src\main.rs:24:9
|
24 | let mut colors = Colors::default(); // Compiler warning variable does not need to be mutable
| ----^^^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
On the other hand, using references I can’t make it to work (same compiler warning):
let colors_tuple = vec![
("background", &colors.bg),
("foreground", &colors.fg),
("font", &colors.font),
];
for &(mut tuple) in colors_tuple.iter() {
let hex_string = get_color(tuple.0.to_string());
match u32::from_str_radix(&hex_string, 32) {
Ok(v) => tuple.1 = &v, // How can I change colors.xxx value?
Err(_e) => panic!("BAD HEX NUMBER!"),
}
}
Any help will be appreciated.