#string #join #string-formatting #grammar #list

no-std oxford_join

Join string slices with Oxford Commas!

16 unstable releases (3 breaking)

0.4.1 Nov 28, 2024
0.3.0 Oct 3, 2024
0.2.9 Oct 5, 2023
0.2.8 Jun 1, 2023
0.1.1 Jun 27, 2021

#321 in Text processing

Download history 3/week @ 2024-08-24 131/week @ 2024-08-31 35/week @ 2024-09-07 61/week @ 2024-09-14 78/week @ 2024-09-21 162/week @ 2024-09-28 209/week @ 2024-10-05 39/week @ 2024-10-12 4/week @ 2024-10-19 6/week @ 2024-10-26 35/week @ 2024-11-02 1/week @ 2024-11-09 7/week @ 2024-11-16 190/week @ 2024-11-23 36/week @ 2024-11-30

241 downloads per month
Used in guff_css

WTFPL license

34KB
518 lines

Oxford Join

docs.rs changelog
crates.io ci deps.rs
license contributions welcome

Join a slice of strings with Oxford Commas inserted as necessary, using the Conjunction of your choice.

(You know, as it should be. Haha.)

The return formatting depends on the size of the set:

0: ""
1: "first"
2: "first <CONJUNCTION> last"
n: "first, second, …, <CONJUNCTION> last"

This crate is #![no_std]-compatible.

Examples

The magic is accomplished with the OxfordJoin trait. Import that, and most slice-y things holding AsRef<str> will inherit the OxfordJoin::oxford_join method for joining.

use oxford_join::{Conjunction, OxfordJoin};

let set = ["Apples", "Oranges"];
assert_eq!(set.oxford_join(Conjunction::And), "Apples and Oranges");

let set = ["Apples", "Oranges", "Bananas"];
assert_eq!(set.oxford_join(Conjunction::And), "Apples, Oranges, and Bananas");

// There are also shorthand methods for and, or, and_or, and nor, allowing you
// to skip the Conjunction enum entirely.
assert_eq!(set.oxford_and(), "Apples, Oranges, and Bananas");
assert_eq!(set.oxford_and_or(), "Apples, Oranges, and/or Bananas");
assert_eq!(set.oxford_nor(), "Apples, Oranges, nor Bananas");
assert_eq!(set.oxford_or(), "Apples, Oranges, or Bananas");

That's all, folks!

Installation

Add oxford_join to your dependencies in Cargo.toml, like:

[dependencies]
oxford_join = "0.4.*"

No runtime deps