I have this code:
#[derive(Debug)]
pub struct Counter {
pub calories: f64,
}
pub struct Protein {
pub weight_in_kg: f64,
}
pub struct Fat {
pub weight_in_kg: f64,
}
impl Counter {
fn eat(&mut self, food: T) {
self.calories += food.gives();
}
}
pub trait Food {
fn gives(&self) -> f64;
}
impl Food for Protein {
fn gives(&self) -> f64 {
self.weight_in_kg * 4.0
}
}
impl Food for Fat {
fn gives(&self) -> f64 {
self.weight_in_kg * 9.0
}
}
Which gives this error:
fn eat(&mut self, food: T) { ^ not found in this scope
T can either be Protein or Fat. But I am not sure where to implement T to get it working.