Avatar ·

How to change a struct field from a method in Rust?

Hello, colleagues! I just started learning Rust and encountered a problem that seems simple, but I can't solve it. I have a `Counter` struct that stores a number. I want to write an `increment` method that increases this number by 1. But the compiler gives an error, and I don't understand why. Here's my code: ```rust struct Counter { value: i32, } impl Counter { fn increment(&self) { self.value += 1; // Error! } } fn main() { let mut counter = Counter { value: 0 }; counter.increment(); println!("{}", counter.value); } ``` The compiler says: `cannot borrow `self.value` as mutable, as `self` is a `&` reference`. I thought that if I declare `counter` as `mut`, I could change its fields inside methods. But apparently, that's not the case. How