I have a struct that has many properties. This struct has a function that makes a decision, and passes the appropriate property to a different function that performs arithmetic on the referenced property. Why am I not able to do this kind of multi-leveled reference passing in rust?
https://play.rust-lang.org/?gist=ef0cfc561afc2e4374475b93b2f62ab0&version=stable
struct Foo {
pub a: u8
}
impl Foo {
pub fn new() -> Foo {
Foo {
a: 1
}
}
pub fn calculate(&mut self) {
self.a += 1; // This is perfectly fine
self.add_later(&mut self.a); // This throws an error
}
fn add_later(&mut self, arg: &mut u8) {
*arg += 1;
}
}
fn main() {
let mut bar = Foo::new();
println!("{}", bar.a);
bar.calculate();
println!("{}", bar.a);
}