Skip to content

Audit integer types in the standard library #22240

Closed
@nikomatsakis

Description

@nikomatsakis

Below is a list of modules that contain text matching the regular expression \bu?int\b and which are intended to be stable. In some cases, these terms only appear in comments (which should still be fixed). In some cases, I have simply put .. after a directory to indicate all files within that directory. Please sign up for modules in comments below and I will endeavor to keep this list up to date with who is working on what (if you have sufficient privileges, feel free to edit the comment directly).

Guidelines to follow

These are the guidelines that we intend to follow in the standard library. These guidelines are not intended as universal guidelines to be used outside the standard library (though of course one might choose to do so).

  1. Use unsigned values if the value should always be greater than or equal to zero, and signed values otherwise.
  2. For indices, pointers, or other values which are tied to a data structure whose size is proportional to the size of memory, use usize or isize.
  3. For cases where the acceptable domain of a value perfectly fits a fixed number of bits, use the appropriate fixed-size type. For example, a method like write_u16 would take a u16 argument.
  4. Otherwise, use i32/u32 if the value has a narrow range and i64/u64 otherwise.

Examples:

  • Vector indices and hashmap lengths use usize.
  • The size of a file would be u64, as the maximum size of a file is not tied to addressable memory.
  • Something like write_u16 takes a u16 (shocker, I know!)
  • The radix of an integer would use u32.
    • You might expect u8, since a radix higher than 256 is not useful, but the domain of useful radices is actually much smaller than u8, so using u8 isn't providing a meaningful guarantee, and will simply increase friction.

Module listing

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions