...

/

Solution: Making a Phone Book

Solution: Making a Phone Book

Let's have a look at the solution of making a phone book.

We'll cover the following...

Solution

This exercise is all about storing data in a list and printing your phone book. Here is the complete implementation of the problem. Let’s take a look at it!

Press + to interact
enum PhoneType {
Home, Work, Cell
}
struct Person {
name: String,
phone: String,
phone_type: PhoneType,
}
impl Person {
fn new(name: &str, phone: &str, phone_type: PhoneType) -> Self {
Self {
name: name.to_string(),
phone: phone.to_string(),
phone_type
}
}
fn print(&self) {
println!("{}", self.name);
println!("{} ({})", self.phone, match self.phone_type {
PhoneType::Home => "Home",
PhoneType::Work => "Work",
PhoneType::Cell => "Cell",
});
println!("");
}
}
fn main() {
let people = vec![
Person::new("Jane & John Doe", "555-123-4567", PhoneType::Home),
Person::new("Joseph Bloggs", "555-111-2222", PhoneType::Work),
Person::new("Rowena Stevenson", "555-111-3333", PhoneType::Cell),
Person::new("Marcus Mills", "555-111-4444", PhoneType::Cell),
Person::new("Orson Richards", "555-111-5555", PhoneType::Cell),
];
for person in people.iter() {
person.print();
}
}

Explanation

...