Am I forced to make this mutable or is there another way?
⚓ Rust 📅 2026-04-29 👤 surdeus 👁️ 3I want each instance of the struct to be immutable, but I don't know how to do that and still reuse the variable name for the next go round of the loop. Help?
struct State {
a: i32,
b: i32,
}
fn iterate(s: &State) -> State {
State {
a: s.a + 1,
b: s.b + 1,
}
}
fn main() {
let mut current_state = State { a: 0, b: 100 };
for _ in 0..100 {
current_state = iterate(¤t_state);
println!("a{} b{}", current_state.a, current_state.b);
//current_state.a = 17; // I don't want current state to be mutable here
}
}
4 posts - 4 participants
🏷️ Rust_feed