I created an example.
I create my_city variable. It stores information about the population of the city.
How can I change only the population value?
struct Amsterdam {
population:i32,
timezone:i32,
}
enum City{
Amsterdam(Amsterdam),
}
enum Country{
Netherlands(City),
}
fn main(){
let mut my_city = Country::Netherlands(City::Amsterdam(Amsterdam{
population:821752,
timezone:2
}));
if let Country::Netherlands(City::Amsterdam(Amsterdam {population,..})) = my_city{
println!("{}",population);
};
}
Can I change the population inside "if let"?
Now I do it like this:
let mut my_city = Country::Netherlands(City::Amsterdam(Amsterdam{
population:821752,
timezone:2
}));
my_city = Country::Netherlands(City::Amsterdam(Amsterdam{
population:900000,
timezone:2
}));
if let Country::Netherlands(City::Amsterdam(Amsterdam {population,..})) = my_city{
println!("{}",population);
};
I find this wrong and inconvenient.
I would like like this:
if let Country::Netherlands(City::Amsterdam(Amsterdam {mut population,..})) = my_city{
population = 900000;
println!("{}",population);
};
help me please