Which integer type should I use for representing file sizes

I want to write a struct that stores the filesize of a given file in bytes. Which integer type is the more appropriate one? From my basic understanding, it'll be unsigned because file sizes can never be <0 but I can't get more specific than that. Note that I'm expecting these file sizes to be small (typically 1MB)

Metadata::len returns u64.

3 Likes

Thank you!

yeah, u64 is basically the universally agreed apon answer.

you may occasionally encounter a file larger than 4GB, but you're not gonna have a file that's more than 18 exabytes.

2 Likes

If you're storing the total size of a storage system, you might want u128.

But for a single file, u64 is a good choice.

Linux, Windows, and macOS are not capable of managing files larger than u64 in size. That is generally the correct datatype.