ParseFloatError
RustERRORCommonParsing

Cannot parse float from string

Quick Answer

Validate the string format; handle special values (NaN, inf) explicitly if needed.

What this means

Returned by str::parse::<f64>() when the string is not a valid floating-point number.

Why it happens
  1. 1Non-numeric characters
  2. 2Locale-specific decimal separators (comma instead of dot)
  3. 3Empty string

Fix

Parse float safely

Parse float safely
let input = "3.14";
match input.trim().parse::<f64>() {
    Ok(f) if f.is_finite() => println!("{}", f),
    Ok(f) => eprintln!("Non-finite: {}", f),
    Err(e) => eprintln!("Parse error: {}", e),
}

Why this works

is_finite() guards against NaN/infinity after a successful parse.

Code examples
Triggerrust
"3,14".parse::<f64>().unwrap(); // ParseFloatError
Same error in other languages
Sources

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Rust errors