Guest User

Untitled

a guest
Sep 14th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. fn main() {
  2.  
  3. #[derive(Debug)]
  4. enum HairColor {
  5. Blonde,
  6. Brown,
  7. Dark,
  8. Albino
  9. };
  10.  
  11. #[derive(Debug)]
  12. struct Person {
  13. name: String,
  14. age: i32,
  15. hair_color: HairColor,
  16. id: i32,
  17. };
  18.  
  19. impl Person {
  20. fn new(name: &str, age: i32, hair_color: HairColor) -> Self {
  21. Person {
  22. name: name.to_owned(),
  23. age,
  24. hair_color,
  25. id: 0
  26. }
  27. }
  28.  
  29. fn set_id(&mut self, id: i32) {
  30. self.id = id;
  31. }
  32. }
  33.  
  34. let mut persons = [
  35. Person::new("Snipp", 30, HairColor::Brown),
  36. Person::new("Snapp", 31, HairColor::Brown),
  37. Person::new("Snute", 32, HairColor::Blonde)
  38. ];
  39.  
  40. for (index, p) in persons.iter_mut().enumerate() {
  41. p.set_id(index as i32 + 1);
  42. }
  43. }
Add Comment
Please, Sign In to add comment