For this challenge, we'll be modeling a fitness program.
Create a new Cargo project. Give your project a name of
fitness.
The project will include both a binary crate and a library
crate. Create the required files in the src directory.
We'll have 3 modules: diet, weightlifting, and cardio.
Declare the 3 modules in the library crate root.
Use an inline module definition for the diet module.
Inside the diet module:
-
Declare a NUTRITIONIST constant set to "Norah Nutrition"
-
Declare an
ask_about_programfunction that outputs the text "The nutritionist is Norah Nutrition" (use the constant).
Use a file module definition for the weightlifting module.
Inside the weightlifting module:
-
Declare a PERSONAL_TRAINER constant set to "Will Weight"
-
Declare an
ask_about_programfunction that outputs the text "The weightlifting trainer is Will Weight" (use the constant) -
Declare an Exercise struct with a
namefield (String) and arepsfield (u32). Derive a Debug implementation. -
Add a
newconstructor function to the Exercise struct that returns an Exercise instance.
Use a folder module definition for the cardio module.
Inside the cardio module:
-
Declare a PERSONAL_TRAINER constant set to "Carl Cardio"
-
Declare an
ask_about_programfunction that outputs the text "The cardio trainer is Carl Cardio" (use the constant) -
Declare a CardioTool enum with 2 variants: Treadmill and Bike. Derive a Debug implementation.
-
Declare an Exercise struct with a
dayfield (String), atoolfield (CardioTool), and aminutesfield (u32). Derive a Debug implementation. -
Add a
newconstructor function to the Exercise struct that returns an Exercise instance.
In the library crate root:
-
Use the
usekeyword to bring the CardioTool enum into scope -
Assign the alias
CardioExerciseto cardio::Exercise -
Assign the alias
WeightliftingExerciseto weightlifting::Exercise -
Define a GymWorkout struct with a
cardiofield (an Exercise struct from thecardiomodule) and aweightliftingfield (an Exercise struct from theweightliftingmodule). Implement the Debug trait. //tbd -
Define a
newconstructor function on the GymWorkout struct. The function should invoke ALL 3 of theask_about_programfunctions, then return an instance of the GymWorkout struct. Pick arbitrary values for the required struct fields.
Finally, in the binary crate root:
- Invoke the
GymWorkout::newfunction and print out the GymWorkout struct in Debug format.
Here is what a sample output might look like:
The nutritionist is Norah Nutrition The cardio trainer is Carl Cardio The weightlifting trainer is Will Weight GymWorkout { weightlifting: Exercise { name: "Bench Press", reps: 8, }, cardio: Exercise { day: "Thursday", tool: Bike, minutes: 30, }, }