num-ordinal: ordinal number types in Rust
Ordinal numbers (first, second, third, ...) are usually represented as 0-based or 1-based integers. In English and most other natural languages, they're represented as 1-based numbers: first = 1st, second = 2nd, third = 3rd and so on. However, most programming languages are zero-based, i.e. when getting the first element in array or list, the index is 0. This is also true for Rust.
Usage
To make working with ordinal numbers more explicit and less error-prone, this library provides ordinal number types that can be converted to/from cardinal numbers while specifying if it is 0- or 1-based:
use ;
// Osize is an ordinal usize
let o = from0;
assert_eq!;
let o = from1;
assert_eq!;
There are also two convenience functions to create ordinal numbers when the return type can be inferred:
use ;
// Osize is an ordinal usize
let o: Osize = ordinal0;
assert_eq!;
let o: Osize = ordinal1;
assert_eq!;
And a macro:
use ;
// type is inferred:
let o: O32 = ordinal!;
// type can also be specified:
let o = ordinal!;
Implemented traits
Ordinal numbers implement a number of traits, so they can be compared, hashed, copied and formatted. Also, you can add or subtract an integer from an ordinal number:
use ordinal;
assert_eq!;
Subtracting an ordinal from an ordinal produces an integer:
use ordinal;
assert_eq!;
The default value is first.
Features
serde: ImplementSerializeandDeserializefor ordinals
License
MIT