Skip to content

Instantly share code, notes, and snippets.

@jprudent
Forked from anonymous/playground.rs
Created January 12, 2017 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jprudent/faa44cf1de6a20b1884f71bd8e20b70c to your computer and use it in GitHub Desktop.
Save jprudent/faa44cf1de6a20b1884f71bd8e20b70c 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