Function parse

Source
pub fn parse(s: &str) -> Result<usize, ParseIntError>
Expand description

Parse a numeric string with an optional one-or-two character multiplicative suffix (K, KB, M, MB, …) into an integer. Numeric strings may be decimal, octal, or hexadecimal. Multiple numeric strings may be multipled together by separating them with the ‘X’ character. See [units] for a list of the SI suffixes.

let tt = vec![
    ("base case", "2", Some(2)),
    ("two ok", "0x18X21", Some(0x18 * 21)),
    ("mixed types", "0x11X0o22X2", Some(0x11 * 0o22 * 2)),
    ("multiple suffixes", "0x13MX9K", Some(0x13 * units::M * 9 * units::K)),
    ("bad string", "asjopdajsdomasd", None),
];
for (name, s, want) in tt {
    let got = parse(s).ok();
    assert_eq!(
        want, got,
        r#"{}: want number("{}") = {:?}, but got {:?}"#,
        name, s, want, got
    );
}