PrettySize for rust

A comprehensive file size library

We’ve just published a rust port of our PrettySize.NET library, now available via cargo and github. Like its .NET predecessor, PrettySize-rs aims to provide a comprehensive API for dealing with file sizes, covering both manipulation and human-readable formatting.

Before anything else, here’s a quick example:

let bytes = 42 * size::MiB;
assert_eq!(bytes, 44040192);

let bytes = Size::MiB(42);
println!("{}", bytes); // prints "42.0 MiB"

PrettySize 0.1 currently provides the following, which are pretty much essentials when it comes to interacting with file sizes:

  • Defines for base-two (KiB, MiB, …) and base-ten (MB, GB, …) constants in the size namespace,
  • A zero-cost, arbitrary-precision type to store file sizes (size::Size) annotated with their unit type,1
  • A std::fmt::Display impl for Size that will emit human-readable values for file sizes (as shown above),
  • A Size::format(...) function that provides fine-grained control over the human-readable output, allowing you to format sizes in base-2 or base-10 units, choose between abbreviated and unabbreviated unit names, and lower vs upper case output,
  • A Size::bytes() function to retrieve the equivalent byte count for any Size

The currently provided functions should suffice for most cases involving ingestion of file sizes or outputting formatted file sizes for UI or logging purposes; but more work remains to be done.

As with all of our other contributions, PrettySize-rs has been published under the liberal terms of the MIT public license and is available for immediate (re)use or modification at GitHub. Pull requests are both welcomed and appreciated!

Download PrettySize

[readme] [docs]

  1. Here, arbitrary precision means that it’s actually size::Size<T> where T can be any numeric type. 

  • Similar Posts

    Craving more? Here are some posts a vector similarity search turns up as being relevant or similar from our catalog you might also enjoy.
    1. C# file size formatting library PrettySize 3.1 released
    2. PrettySize: a .NET library for printing human-readable file sizes
    3. PrettySize 0.3 release and a weakness in rust's type system
    4. Using to integrate rust applications with system libraries like a pro
  • Leave a Reply

    Your email address will not be published. Required fields are marked *