Guest User

Untitled

a guest
Oct 11th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #[derive(Debug)]
  2. enum Relationship {
  3. PARENT,
  4. CHILD,
  5. }
  6.  
  7. #[derive(Debug)]
  8. struct Person {
  9. relationship: Relationship,
  10. name: String,
  11. age: u8,
  12. }
  13.  
  14. impl Person {
  15. fn new(relationship: Relationship, name: &str, age: u8) -> Person {
  16. Person {
  17. relationship,
  18. name: name.to_string(),
  19. age,
  20. }
  21. }
  22. }
  23.  
  24. fn main() {
  25. let mut persons = vec![
  26. Person::new(Relationship::PARENT, "foo", 65),
  27. Person::new(Relationship::CHILD, "foo jr", 25),
  28. Person::new(Relationship::PARENT, "bar", 50),
  29. Person::new(Relationship::CHILD, "bar jr", 20),
  30. ];
  31. for i in 0..(persons.len() - 1) {
  32. let mut first = persons.get_mut(i).unwrap();
  33. let mut second = persons.get_mut(i+1).unwrap();
  34. first.age /= 5;
  35. second.age += 1;
  36. }
  37. println!("{:?}", persons);
  38. }
Add Comment
Please, Sign In to add comment