oxford_join

Crates.iooxford_join
lib.rsoxford_join
version
sourcesrc
created_at2021-06-09 05:35:14.386965
updated_at2024-11-28 19:00:31.901921
descriptionJoin string slices with Oxford Commas!
homepage
repositoryhttps://github.com/Blobfolio/oxford_join
max_upload_size
id408050
Cargo.toml error:TOML parse error at line 26, column 1 | 26 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Josh (joshstoik1)

documentation

README

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.*"
Commit count: 120

cargo fmt