0

I am new to Rust and i am trying to build a something to convert hours and secs to a total amount of mins, but it is giving me this error which I do not know what to do with

thread 'main' panicked at 'called Result::unwrap() on an Err value: ParseFloatError { kind: Invalid }', src/main.rs:22:39 stack backtrace:

Here is the code for the script:

//geting user input
let mut line = String::new();
println!("Enter your hours:miniuts:seconds... ");
std::io::stdin().read_line(&mut line).unwrap();
//spliting string to get hours minutes seconds as different entries in this var
let h_m_s: Vec<&str> = line.split(':').collect();

//convert array to seperate vars
let hours_str = h_m_s[0].to_string();
let mins_str = h_m_s[1].to_string();
let sec_str = h_m_s[2].to_string();

//convert str to int
let hours = hours_str.parse::<f32>().unwrap();
let mins = mins_str.parse::<f32>().unwrap();
let secs = sec_str.parse::<f32>().unwrap();

//now converting to minutes nearing the two decimal places (you know hopefully:))
let hours_to_min = hours * 60.00;
let secs_to_min = secs / 60.00;
let final_result = hours_to_min + mins + secs_to_min;
println!("Here are the minutes: {:.2}", final_result);

I have tried looking it up but nothing comes up that is use-full in my use case.

I also have determined that it is the let secs = sec_str.parse::<f32>().unwrap(); var that is the problem but i am not sure why.

Ark04
  • 9
  • 4

1 Answers1

0

The ParseFloatError { kind: Invalid } could happen because the string you are trying to parse as a f32 is not valid, for example an empty space at the end of the seconds, like in your case.

To avoid this error, trim the variables and use the parse method with a Result return type and handle the error gracefully, for example:

fn main() {

    //geting user input
    let mut line = String::new();
    println!("Enter your hours:miniuts:seconds... ");
    std::io::stdin().read_line(&mut line).unwrap();
    //spliting string to get hours minutes seconds as different entries in this var
    let h_m_s: Vec<&str> = line.split(':').collect();

    //convert array to seperate vars
    let hours_str = h_m_s[0].to_string();
    let mins_str = h_m_s[1].to_string();
    let sec_str = h_m_s[2].to_string();
    let s: String = sec_str.to_string();
    let sec_str = s.trim();

    println!("Hours: {}, Minutes: {}, Seconds: {}", hours_str, mins_str, sec_str);

    let hours = match hours_str.parse::<f32>() {
        Ok(hours) => hours,
        Err(_) => {
            println!("Invalid input for hours");
            return;
        }
    };
    
    let mins = match mins_str.parse::<f32>() {
        Ok(mins) => mins,
        Err(_) => {
            println!("Invalid input for minutes");
            return;
        }
    };
    
    let secs = match sec_str.parse::<f32>() {
        Ok(secs) => secs,
        Err(_) => {
            println!("Invalid input for seconds");
            return;
        }
    };

    //now converting to minutes nearing the two decimal places (you know hopefully:))
    let hours_to_min = hours * 60.00;
    let secs_to_min = secs / 60.00;
    let final_result = hours_to_min + mins + secs_to_min;
    println!("Here are the minutes: {:.2}", final_result);
}

Roniel López
  • 403
  • 2
  • 10
  • I tried your suggestion and I got the same error as before. what i used as the input was "1:2:3" what would you think the type error is coming from? Thanks for the help. – Ark04 Jan 07 '23 at 18:20
  • the problem is that for the seconds you need to trim the text, I edited the code in my answer with the needed changes :) – Roniel López Jan 07 '23 at 18:34