Hello
I added mut to anywhere that follow a variable but it still complain:
error[E0308]: mismatched types
--> src/main.rs:17:18
|
17 | c.add_to_b(&b);
| ^^ types differ in mutability
|
= note: expected type `&mut b::B`
found type `&b::B`
Code is:
pub struct A {
a: i16
}
impl A {
pub fn new() -> A {
return A{a:0};
}
pub fn add(&mut self, i:i16) {
self.a += i;
}
pub fn get(&self) -> i16 {
return self.a;
}
}
pub struct B {
b: i16
}
impl B {
pub fn new() -> B {
return B{b:0};
}
pub fn add(&mut self, i:i16) {
self.b += i;
}
pub fn addFromA(&mut self, a: A) {
self.b += a.get();
}
pub fn get(&self) -> i16 {
return self.b;
}
}
pub struct C {
a: A,
b: B
}
impl C {
pub fn new() -> C {
let mut a = A::new();
let mut b = B::new();
return C{a: a, b: b};
}
pub fn add(&mut self, i:i16) {
self.a.add(i);
self.b.add(i);
}
pub fn add_a_to_b(&self, b: &mut B) {
b.addFromA(self.a);
}
pub fn get(&self) -> i16 {
return self.a.get() + self.b.get();
}
}
fn main() {
println!("Hello, world!");
let mut c = C::new();
let mut a = A::new();
let mut b = B::new();
a.add(3);
b.addFromA(a);
c.add(4);
c.add(b.get());
c.add_a_to_b(&b);
println!("{}", c.get());
println!("{}", b.get());
}