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:
// TODO: remove this when you're done with your implementation.
#![allow(unused_variables, dead_code)]
struct User {
name: String,
age: u32,
weight: f32,
}
impl User {
pub fn new(name: String, age: u32, weight: f32) -> Self {
unimplemented!()
}
pub fn name(&self) -> &str {
unimplemented!()
}
pub fn age(&self) -> u32 {
unimplemented!()
}
pub fn weight(&self) -> f32 {
unimplemented!()
}
pub fn set_age(&mut self, new_age: u32) {
unimplemented!()
}
pub fn set_weight(&mut self, new_weight: f32) {
unimplemented!()
}
}
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_weight() {
let bob = User::new(String::from("Bob"), 32, 155.2);
assert_eq!(bob.weight(), 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);
}