健康统计
你正在实现一个健康监控系统。作为其中的一部分,你需要对用户的健康统计数据进行追踪。
User
结构体的定义和 impl
块中一些函数的框架已经给出。你的目标是实现在 impl
块中定义的 User
struct
的方法。
将以下代码复制到 https://play.rust-lang.org/,并填充缺失的方法:
// TODO: remove this when you're done with your implementation. #![allow(unused_variables, dead_code)] pub struct User { name: String, age: u32, height: f32, visit_count: usize, last_blood_pressure: Option<(u32, u32)>, } pub struct Measurements { height: f32, blood_pressure: (u32, u32), } pub struct HealthReport<'a> { patient_name: &'a str, visit_count: u32, height_change: f32, blood_pressure_change: Option<(i32, i32)>, } impl User { pub fn new(name: String, age: u32, height: f32) -> Self { todo!("Create a new User instance") } pub fn name(&self) -> &str { todo!("Return the user's name") } pub fn age(&self) -> u32 { todo!("Return the user's age") } pub fn height(&self) -> f32 { todo!("Return the user's height") } pub fn doctor_visits(&self) -> u32 { todo!("Return the number of time the user has visited the doctor") } pub fn set_age(&mut self, new_age: u32) { todo!("Set the user's age") } pub fn set_height(&mut self, new_height: f32) { todo!("Set the user's height") } pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport { todo!("Update a user's statistics based on measurements from a visit to the doctor") } } fn main() { let bob = User::new(String::from("Bob"), 32, 155.2); println!("I'm {} and my age is {}", bob.name(), bob.age()); } #[test] fn test_height() { let bob = User::new(String::from("Bob"), 32, 155.2); assert_eq!(bob.height(), 155.2); } #[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))); }