Format a byte size in human-readable form. Automatically selects the appropriate unit (bytes, KiB, MiB, GiB). # Arguments `bytes` - The size in bytes # Returns A formatted string like "1.5 MiB" or "42 bytes". # Example ```rust,ignore assert_eq!(format_size(1024), "1.0 KiB"); assert_eq!(format_size(42), "42 bytes"); ```
(bytes: u64)
| 656 | /// assert_eq!(format_size(42), "42 bytes"); |
| 657 | /// ``` |
| 658 | pub fn format_size(bytes: u64) -> String { |
| 659 | const KIB: u64 = 1024; |
| 660 | const MIB: u64 = 1024 * 1024; |
| 661 | const GIB: u64 = 1024 * 1024 * 1024; |
| 662 | |
| 663 | if bytes >= GIB { |
| 664 | format!("{:.1} GiB", bytes as f64 / GIB as f64) |
| 665 | } else if bytes >= MIB { |
| 666 | format!("{:.1} MiB", bytes as f64 / MIB as f64) |
| 667 | } else if bytes >= KIB { |
| 668 | format!("{:.1} KiB", bytes as f64 / KIB as f64) |
| 669 | } else { |
| 670 | format!("{} B", bytes) |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /// Format a count with singular/plural label. |
| 675 | /// |
nothing calls this directly
no outgoing calls
no test coverage detected