Skip to main content

parse_formula

Function parse_formula 

Source
pub fn parse_formula(formula: &str) -> FormulaResult<Vec<(u32, f64)>>
Expand description

Parse a chemical formula into per-element atom counts.

Returns (Z, count) pairs sorted by atomic number with duplicate elements aggregated (so "CH3(CH2)6CH3" yields [(6, 4.0), (1, 18.0)]).

Supported grammar: nested parenthesized groups with multi-digit group counts, one/two-letter case-sensitive element symbols, multi-digit atom counts, and trailing hydrate parts joined by · (or plain .), each optionally prefixed by an integer multiplier (CuSO4·5H2O). Surrounding whitespace is tolerated; embedded whitespace, a leading digit (bare stoichiometric coefficients are not formulas), stray parentheses, and unrecognized symbols are errors.