Skip to main content

parse_memory_string

Function parse_memory_string 

Source
pub fn parse_memory_string(s: &str) -> Result<u64, String>
Expand description

Parse memory string like “512Mi”, “1Gi” to bytes

Supports both IEC (binary) and SI (decimal) units:

  • IEC: Ki, Mi, Gi, Ti (powers of 1024)
  • SI: K/k, M/m, G/g, T/t (powers of 1000)
  • No suffix: bytes

§Examples

assert_eq!(parse_memory_string("512Mi").unwrap(), 512 * 1024 * 1024);
assert_eq!(parse_memory_string("1Gi").unwrap(), 1024 * 1024 * 1024);
assert_eq!(parse_memory_string("2G").unwrap(), 2 * 1000 * 1000 * 1000);

§Errors

Returns an error if the string cannot be parsed as a memory size.