I have this simple code to get the BMI (starting to learn Rust). It works fine.
use std::io;
fn main() {
println!("***Rust BMI***");
let mut w: f32 = 0.0;
let mut weight = String::new();
println!("Inform your weight: ");
io::stdin()
.read_line(&mut weight)
.expect("Failed to read weight");
w = weight.trim().parse().expect("Not a number");
println!("Your weight is {}kg", w);
let mut h: f32 = 0.0;
let mut height = String::new();
println!("Inform your height: ");
io::stdin()
.read_line(&mut height)
.expect("Failed to read height");
h = height.trim().parse().expect("Not a valid number");
println!("Your height is {}m", h);
println!("Your bmi is {}", bmi(w, h));
println!("You are: {}", bmi_res(w, h));
}
fn bmi(weight: f32, height: f32) -> f32 {
weight / (height * height)
}
fn bmi_res(weight: f32, height: f32) -> &'static str {
let index = weight as f32 / height.powi(2);
match index {
index if index <= 18.5 => "Underweight",
index if index <= 25.0 => "Normal",
index if index <= 30.0 => "Overweight",
_ => "Obese",
}
}
My question is: how would I check only for numbers (float specially)? If the user informs anything different from that, the program crashes. I’ve tried if, but to no avail. Also tried match, but couldn't validate the input. I don’t know and could not find anything “is_float()” like method.
What I mean is: if the user informs anything different from number, he's supposed to be redirected to the same question, "Inform your weight/height", and not have the program crashed.
Here is the repo.