Crates.io | oxford_join |
lib.rs | oxford_join |
version | |
source | src |
created_at | 2021-06-09 05:35:14.386965 |
updated_at | 2024-11-28 19:00:31.901921 |
description | Join string slices with Oxford Commas! |
homepage | |
repository | https://github.com/Blobfolio/oxford_join |
max_upload_size | |
id | 408050 |
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` |
size | 0 |
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.
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!
Add oxford_join
to your dependencies
in Cargo.toml
, like:
[dependencies]
oxford_join = "0.4.*"