Skip to content

Instantly share code, notes, and snippets.

@luismendes070
Last active March 3, 2024 13:30
Show Gist options
  • Save luismendes070/4d6ba7823124e1f75a1c6c4b57413f36 to your computer and use it in GitHub Desktop.
Save luismendes070/4d6ba7823124e1f75a1c6c4b57413f36 to your computer and use it in GitHub Desktop.
Índice massa corporal #Copilot
IMC Rust #Copilot
@luismendes070
Copy link
Author

luismendes070 commented Mar 3, 2024

IMC Rust

In this version, the read_input function reads a line from the standard input and tries to parse it as a floating-point number. If the parsing is successful and the number is greater than zero, it returns Ok(number). If the parsing fails or the number is not greater than zero, it returns an error.

The main function then uses pattern matching to check if both the weight and height were read successfully. If they were, it calculates and prints the BMI. If there was an error, it prints an error message.

Please note that this is still a very basic implementation. In a real-world application, you might want to add more sophisticated error handling and input validation. For example, you could check if the weight and height are within reasonable ranges, or you could give the user multiple attempts to enter a valid number. You could also improve the error messages to make them more informative and user-friendly. Always remember to consult a healthcare professional for accurate health information.

@luismendes070
Copy link
Author

Body Mass Index(BMI) include any error handling or input validation
BMI Rust Playground

@luismendes070
Copy link
Author

In this version, the read_input function returns a Result<f32, &'static str>. If the parsing fails or the number is not greater than zero, it returns an error with a custom error message. The main function then checks the results of read_input separately for weight and height, and prints an appropriate error message if there was an error. This way, the user will know whether the error was with the weight or the height. I hope this helps! Let me know if you have any other questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment