Skip to content

Instantly share code, notes, and snippets.

Created January 12, 2017 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/489464b391a2fb318bfd4c23e992e5b2 to your computer and use it in GitHub Desktop.
Save anonymous/489464b391a2fb318bfd4c23e992e5b2 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
struct Element {
x: u8
}
impl Element {
pub fn inc(&mut self, container: &Container) {
self.x = self.x + 1;
}
}
struct Container {
v: Vec<Element>
}
impl Container {
/*
pub fn inc_all_elem(&mut self) {
for e in self.v {
e.inc();
}
}
*/
pub fn inc_all_elem(&mut self) {
for e in self.v.iter_mut() {
e.inc(self);
}
}
}
fn main() {
let mut container = Container {
v: vec!(Element{x:1})
};
container.inc_all_elem();
println!("container.v[0] = {}", container.v[0].x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment