## ByteSize
[](https://crates.io/crates/bytesize)
[](https://docs.rs/bytesize/2.5.0)


<br />
[](https://deps.rs/crate/bytesize/2.5.0)
[](https://crates.io/crates/bytesize)
`ByteSize` is a semantic wrapper for byte count representations.
Features:
- Pre-defined constants for various size units (e.g., B, KB, KiB, MB, MiB, ... EB, EiB).
- `ByteSize` type which presents size units convertible to different size units.
- Arithmetic operations for `ByteSize`.
- `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5KiB" and "521TiB".
- Serde support for binary and human-readable deserializers like JSON.
### Feature flags
- `std` (default): Enables the `alloc` feature and standard library optimizations.
- `alloc`: Enables parsing and formatting.
- `arbitrary`: Implements `arbitrary::Arbitrary` for [`ByteSize`].
- `serde`: Enables `alloc` and implements serialization and deserialization for [`ByteSize`].
Disable the default features to use only the core data types, conversions, and constants.
### Examples
Construction using SI or IEC helpers.
```rust
use bytesize::ByteSize;
assert!(ByteSize::kib(4) > ByteSize::kb(4));
```
Display as human-readable string.
```rust
use bytesize::ByteSize;
assert_eq!("518.0 GiB", ByteSize::gib(518).display().iec().to_string());
assert_eq!("556.2 GB", ByteSize::gib(518).display().si().to_string());
assert_eq!("518.0G", ByteSize::gib(518).display().iec_short().to_string());
```
Arithmetic operations are supported.
```rust
use bytesize::ByteSize;
let plus = ByteSize::mb(1) + ByteSize::kb(100);
println!("{plus}");
let minus = ByteSize::tb(1) - ByteSize::gb(4);
assert_eq!(ByteSize::gb(996), minus);
```