1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-21 08:19:32 +02:00

Add solution for Health Statistics exercise (#1172)

This commit is contained in:
Frances Wingerter
2023-09-26 07:29:00 +00:00
committed by GitHub
parent fc388570bf
commit 758639174a
4 changed files with 138 additions and 45 deletions

View File

@ -16,6 +16,10 @@ path = "day-1/luhn.rs"
name = "book-library"
path = "day-2/book-library.rs"
[[bin]]
name = "health-statistics"
path = "../../third_party/rust-on-exercism/health-statistics.rs"
[[bin]]
name = "strings-iterators"
path = "day-2/strings-iterators.rs"

View File

@ -9,45 +9,41 @@ methods:
// TODO: remove this when you're done with your implementation.
#![allow(unused_variables, dead_code)]
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs}}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:setup}}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_new}}
todo!("Create a new User instance")
}
fn main() {
let bob = User::new(String::from("Bob"), 32, 155.2);
println!("I'm {} and my age is {}", bob.name(), bob.age());
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_name}}
todo!("Return the user's name")
}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_age}}
todo!("Return the user's age")
}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_height}}
todo!("Return the user's height")
}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_doctor_visits}}
todo!("Return the number of time the user has visited the doctor")
}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_set_age}}
todo!("Set the user's age")
}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_set_height}}
todo!("Set the user's height")
}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:User_visit_doctor}}
todo!("Update a user's statistics based on measurements from a visit to the doctor")
}
}
#[test]
fn test_height() {
let bob = User::new(String::from("Bob"), 32, 155.2);
assert_eq!(bob.height(), 155.2);
}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:main}}
#[test]
fn test_set_age() {
let mut bob = User::new(String::from("Bob"), 32, 155.2);
assert_eq!(bob.age(), 32);
bob.set_age(33);
assert_eq!(bob.age(), 33);
}
#[test]
fn test_visit() {
let mut bob = User::new(String::from("Bob"), 32, 155.2);
assert_eq!(bob.doctor_visits(), 0);
let report = bob.visit_doctor(Measurements {
height: 156.1,
blood_pressure: (120, 80),
});
assert_eq!(report.patient_name, "Bob");
assert_eq!(report.visit_count, 1);
assert_eq!(report.blood_pressure_change, None);
let report = bob.visit_doctor(Measurements {
height: 156.1,
blood_pressure: (115, 76),
});
assert_eq!(report.visit_count, 2);
assert_eq!(report.blood_pressure_change, Some((-5, -4)));
}
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:tests}}
```

View File

@ -7,3 +7,11 @@
```rust
{{#include book-library.rs:solution}}
```
## Health Statistics
([back to exercise](health-statistics.md))
```rust
{{#include ../../../third_party/rust-on-exercism/health-statistics.rs:solution}}
```