ordinal-trait

Crates.ioordinal-trait
lib.rsordinal-trait
version0.1.1
created_at2024-11-05 08:34:45.360613+00
updated_at2025-04-21 21:43:10.924625+00
descriptionFormat numbers as ordinals efficiently.
homepage
repositoryhttps://github.com/heaths/ordinal-rs
max_upload_size
id1436233
size159,586
Heath Stewart (heaths)

documentation

README

Ordinal formatting

⚠️ Deprecated ⚠️

Use ordinal. See history for more information.

Format numbers as ordinals efficiently. You can get the ordinal suffix e.g., "st", "nd", "rd", or "th" without allocations.

Examples

Format a number as an ordinal, allocating a new String:

use ordinal_trait::Ordinal as _;
assert_eq!(12.to_ordinal(), "12th");

Get a number representing an ordinal you can use with comparisons and formatting.

use ordinal_trait::Ordinal as _;
let n = 12.to_number();
assert_eq!(*n, 12);
assert_eq!(format!("{n}"), "12th");

Performance

Compared to most other implementations that allocate a string just to check the last one or two characters, this implementation is much faster and does not allocate a string1.

violin plot

To compare measurements across branches:

git checkout main
cargo bench -- --save-baseline main

git checkout feature
cargo bench -- --baseline main

Footnotes

  1. Criterion does not have built-in memory profiling but when I find an impl of Measurement to do so - or find time to write one - I'll include those stats as well; however, take into consideration that this implementation does not allocate a string at all for suffix().

Commit count: 31

cargo fmt