pub fn parse_size(size: &str) -> Result<usize, ParseSizeError>
Expand description

Parse a size string into a number of bytes.

A size string comprises an integer and an optional unit. The unit may be K, M, G, T, P, E, Z or Y (powers of 1024), or KB, MB, etc. (powers of 1000), or b which is 512. Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

Errors

Will return ParseSizeError if it’s not possible to parse this string into a number, e.g. if the string does not begin with a numeral, or if the unit is not one of the supported units described in the preceding section.

Examples

use uucore::parse_size::parse_size;
assert_eq!(Ok(123), parse_size("123"));
assert_eq!(Ok(9 * 1000), parse_size("9kB")); // kB is 1000
assert_eq!(Ok(2 * 1024), parse_size("2K")); // K is 1024