- 
      
 - 
        
Save jprudent/faa44cf1de6a20b1884f71bd8e20b70c to your computer and use it in GitHub Desktop.  
    Shared via Rust Playground
  
        
  
    
      This file contains hidden or 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 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