Expand description
§Oxford Join
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");
There is also a Display
-based OxfordJoinFmt
wrapper
that can be more efficient for format!
-type use cases, or types which
implement Display
but not AsRef<str>
.
use oxford_join::OxfordJoinFmt;
let set = ["Apples", "Oranges", "Bananas"];
assert_eq!(
format!("I eat {}.", OxfordJoinFmt::and(&set)),
"I eat Apples, Oranges, and Bananas.",
);
That’s all, folks!
Structs§
- JoinFmt
Display
-Based Join Wrapper.- Oxford
Join Fmt Display
-Based Oxford Join Wrapper.
Enums§
- Conjunction
- Conjunction.
Traits§
- Oxford
Join - Oxford Join.