fn main() {
// Initialize distance and speed
let distance: f64 = 30.0; // (in km)
let speed: f64 = 90.0; // (in km/h)
// Check to avoid division by zero
if speed <= 0.0 {
eprintln!("Speed must be greater than zero.");
std::process::exit(1); // exit with error code
}
// Calculate time
let tm: f64 = distance / speed;
println!("Time required: {:.2} hours", tm);
// Optional: convert to hours and minutes
let hours: i32 = tm.floor() as i32;
let minutes: i32 = ((tm - hours as f64) * 60.0).round() as i32;
println!(
"Which is approximately {} hours and {} minutes.",
hours, minutes
);
}
/*
run:
Time required: 0.33 hours
Which is approximately 0 hours and 20 minutes.
*/