parse_size
Parses a human-written size such as "1.5 KiB", "10MB" or "512" into a number of bytes.
The number is a non-negative decimal (a fraction is allowed, at most 18 decimal digits are used, and the result is rounded down), optionally followed by whitespace and a unit, matched without regard to case:
- none or
B: bytes; KiB,MiB,GiB,TiB,PiB,EiB, and the single lettersK,M,G,T,P,E: powers of 1024;KB,MB,GB,TB,PB,EB: powers of 1000.
format_size writes the binary form back.
Import
Section titled “Import”Cargo feature bytes (enabled by default). To compile only this module:
or in Cargo.toml:
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
input | &str | The size to parse. |
Returns
Section titled “Returns”Result<u64, ParseSizeError> — Ok on success, otherwise an Err: see Errors.
Errors
Section titled “Errors”Returns a ParseSizeError for an empty string, something other than a number where one is
expected, an unknown unit, or a size that does not fit in a u64.
Examples
Section titled “Examples”Error type: ParseSizeError
Section titled “Error type: ParseSizeError”Why a string could not be parsed as a size.
