Health Statistics

You’re working on implementing a health-monitoring system. As part of that, you need to keep track of users’ health statistics.

You’ll start with some stubbed functions in an impl block as well as a User struct definition. Your goal is to implement the stubbed out methods on the User struct defined in the impl block.

Copy the code below to https://play.rust-lang.org/ and fill in the missing methods:

  1. // TODO: remove this when you're done with your implementation.
  2. #![allow(unused_variables, dead_code)]
  3. struct User {
  4. name: String,
  5. age: u32,
  6. weight: f32,
  7. }
  8. impl User {
  9. pub fn new(name: String, age: u32, weight: f32) -> Self {
  10. unimplemented!()
  11. }
  12. pub fn name(&self) -> &str {
  13. unimplemented!()
  14. }
  15. pub fn age(&self) -> u32 {
  16. unimplemented!()
  17. }
  18. pub fn weight(&self) -> f32 {
  19. unimplemented!()
  20. }
  21. pub fn set_age(&mut self, new_age: u32) {
  22. unimplemented!()
  23. }
  24. pub fn set_weight(&mut self, new_weight: f32) {
  25. unimplemented!()
  26. }
  27. }
  28. fn main() {
  29. let bob = User::new(String::from("Bob"), 32, 155.2);
  30. println!("I'm {} and my age is {}", bob.name(), bob.age());
  31. }
  32. #[test]
  33. fn test_weight() {
  34. let bob = User::new(String::from("Bob"), 32, 155.2);
  35. assert_eq!(bob.weight(), 155.2);
  36. }
  37. #[test]
  38. fn test_set_age() {
  39. let mut bob = User::new(String::from("Bob"), 32, 155.2);
  40. assert_eq!(bob.age(), 32);
  41. bob.set_age(33);
  42. assert_eq!(bob.age(), 33);
  43. }