https://github.com/blobfolio/oxford_join
A Rust crate providing a trait to join string slices with Oxford Commas.
https://github.com/blobfolio/oxford_join
grammar rust string-concatenation
Last synced: 9 months ago
JSON representation
A Rust crate providing a trait to join string slices with Oxford Commas.
- Host: GitHub
- URL: https://github.com/blobfolio/oxford_join
- Owner: Blobfolio
- License: wtfpl
- Created: 2021-06-09T05:32:09.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2025-02-26T03:51:24.000Z (over 1 year ago)
- Last Synced: 2025-04-12T13:45:52.233Z (about 1 year ago)
- Topics: grammar, rust, string-concatenation
- Language: Rust
- Homepage:
- Size: 66.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Oxford Join
[](https://docs.rs/oxford_join/)
[](https://github.com/Blobfolio/oxford_join/blob/master/CHANGELOG.md)
[](https://crates.io/crates/oxford_join)
[](https://github.com/Blobfolio/oxford_join/actions)
[](https://deps.rs/crate/oxford_join/)
[](https://en.wikipedia.org/wiki/WTFPL)
[](https://github.com/Blobfolio/oxford_join/issues)
Join a slice of strings with [Oxford Commas](https://en.wikipedia.org/wiki/Serial_comma) 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 last"
n: "first, second, …, 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` will inherit the `OxfordJoin::oxford_join`
method for joining.
```rust
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:
```toml
[dependencies]
oxford_join = "0.7.*"
```