Skip to main content

parse_line_of_n

Function parse_line_of_n 

Source
pub fn parse_line_of_n<T: FromStr>(
    line: &str,
    n: usize,
) -> Result<Vec<T>, ParseError>
where ParseError: From<<T as FromStr>::Err>,
Expand description

Parses a line of whitespace-separated values into a vector of a specific type.

This generic helper function takes a string slice, splits it by whitespace, and attempts to parse each substring into the target type T. The type T must implement std::str::FromStr.

§Arguments

  • line - A string slice representing a single line of data.
  • n - The exact number of values expected on the line.

§Errors

  • ParseError::InvalidVectorLength if the number of parsed values is not equal to n.
  • Propagates any error from the parse() method of the target type T.

§Example

use readcon_core::parser::parse_line_of_n;
let line = "10.5 20.0 30.5";
let values: Vec<f64> = parse_line_of_n(line, 3).unwrap();
assert_eq!(values, vec![10.5, 20.0, 30.5]);

let result = parse_line_of_n::<i32>(line, 2);
assert!(result.is_err());