Skip to content

Instantly share code, notes, and snippets.

Created November 3, 2016 22:01
Show Gist options
  • Save anonymous/574bddb86d6a9f776012478fb73eb5e3 to your computer and use it in GitHub Desktop.
Save anonymous/574bddb86d6a9f776012478fb73eb5e3 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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