-
-
Save boustrophedon/1490601641ddd43435ef6ae7a977de5e to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Thing { | |
pub num: i32, | |
} | |
impl Thing { | |
pub fn new(n: i32) -> Thing { | |
Thing { num: n } | |
} | |
pub fn add_total(&mut self, things: &ThingContainer) { | |
self.num += things.sum_all(); | |
} | |
} | |
struct ThingContainer { | |
thing1: Thing, | |
thing2: Thing, | |
} | |
impl ThingContainer { | |
pub fn new() -> ThingContainer { | |
ThingContainer { | |
thing1: Thing::new(5), | |
thing2: Thing::new(2), | |
} | |
} | |
pub fn sum_all(&self) -> i32 { | |
self.thing1.num + self.thing2.num | |
} | |
pub fn do_stuff(&mut self) { | |
self.thing1.add_total(self); | |
} | |
} | |
fn main() { | |
let mut things = ThingContainer::new(); | |
things.do_stuff(); | |
assert!(things.sum_all() == (5 + (5 + 2)) + 2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment