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)]
4. struct User {
5. name: String,
6. age: u32,
7. weight: f32,
8. }
10. impl User {
11. pub fn new(name: String, age: u32, weight: f32) -> Self {
12. unimplemented!()
13. }
15. pub fn name(&self) -> &str {
16. unimplemented!()
17. }
19. pub fn age(&self) -> u32 {
20. unimplemented!()
21. }
23. pub fn weight(&self) -> f32 {
24. unimplemented!()
25. }
27. pub fn set_age(&mut self, new_age: u32) {
28. unimplemented!()
29. }
31. pub fn set_weight(&mut self, new_weight: f32) {
32. unimplemented!()
33. }
34. }
36. fn main() {
37. let bob = User::new(String::from("Bob"), 32, 155.2);
38. println!("I'm {} and my age is {}", bob.name(), bob.age());
39. }
41. #[test]
42. fn test_weight() {
43. let bob = User::new(String::from("Bob"), 32, 155.2);
44. assert_eq!(bob.weight(), 155.2);
45. }
47. #[test]
48. fn test_set_age() {
49. let mut bob = User::new(String::from("Bob"), 32, 155.2);
50. assert_eq!(bob.age(), 32);
51. bob.set_age(33);
52. assert_eq!(bob.age(), 33);
53. }
