1 2 3 4 5 6 7 8 9 10 11
pub fn format_bytes(mut bytes: usize) -> String { const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"]; let mut unit = 0; while bytes >= 1024 && unit < UNITS.len() - 1 { bytes /= 1024; unit += 1; } format!("{}{}", bytes, UNITS[unit]) }