Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- #[derive(Debug)]
- enum HairColor {
- Blonde,
- Brown,
- Dark,
- Albino
- };
- #[derive(Debug)]
- struct Person {
- name: String,
- age: i32,
- hair_color: HairColor,
- id: i32,
- };
- impl Person {
- fn new(name: &str, age: i32, hair_color: HairColor) -> Self {
- Person {
- name: name.to_owned(),
- age,
- hair_color,
- id: 0
- }
- }
- fn set_id(&mut self, id: i32) {
- self.id = id;
- }
- }
- let mut persons = [
- Person::new("Snipp", 30, HairColor::Brown),
- Person::new("Snapp", 31, HairColor::Brown),
- Person::new("Snute", 32, HairColor::Blonde)
- ];
- for (index, p) in persons.iter_mut().enumerate() {
- p.set_id(index as i32 + 1);
- }
- }
Add Comment
Please, Sign In to add comment