A Rust library for generically joining iterables with a separator

Overview

Travis (.com) GitHub stars Crates.io docs.rs Github commits (since latest release) license

joinery

A Rust library for generically joining iterables with a separator. Provides the tragically missing string join functionality to rust.

extern crate joinery;

use std::env;

// Or use joinery::prelude::*;
use joinery::Joinable;

fn main() {
	// Join things!
	println!("{}", ["Hello", "World!"].iter().join_with(", "));

	// Join things of different types!
	println!("{}", ["Hello", "World!"].iter().join_with(' '));
	println!("{}", (0..10).join_with(' '));
}

Installation

Add joinery to your Cargo.toml:

[dependencies]
joinery = "2.0.0"

Nightly-only features

joinery supports various nightly-only optimization features, such as iter::TrustedLen. These features are enabled with the "nightly" cargo feature:

[dependencies.joinery]
version = "2.0.0"
features = ["nightly"]

Note that, because nightly-only features are unstable, joinery can't make any stability guarentees about their continued presence or interface consistency between versions. While we'll do our best to preserve compatibility, we only guarentee semver compatibility for the non-nightly interface.

Overview

Joinery provides joins over iterators. Put simply, a join is a combination of an iterator and a separator. The join then conceptually represents all the elements of the iterator, with the separator between each one.

You can create a join with the join_with method, which is defined for all IntoIterator types via the Joinable trait:

use joinery::prelude::*;

let content = vec![1, 2, 3, 4, 5];

let join = content.iter().join_with(", ");

Joins implement Display, so they can be written to writers or converted into strings. Joins are stateless, so they can be reused, assuming that the underlying iterator is cloneable:

println!("Comma-separated numbers: {}", join);

let mut result = String::new();
write!(&mut result, "{}", join);

// Don't forget that `Display` gives you `ToString` for free!
let result2 = join.to_string();

Joins are also iterable. You can use .iter() to iterate over references to the underlying iterator, or into_iter() to convert the join into an iterator. Join iterators use an item type called JoinItem, which distinguishes between separators and elements of the underlying iterator:

use joinery::JoinItem;
let mut iter = join.iter();

assert_eq!(iter.next(), Some(JoinItem::Element(&1)));
assert_eq!(iter.next(), Some(JoinItem::Separator(&", ")));
assert_eq!(iter.next(), Some(JoinItem::Element(&2)));
assert_eq!(iter.next(), Some(JoinItem::Separator(&", ")));
assert_eq!(iter.next(), Some(JoinItem::Element(&3)));
You might also like...
A command-line tool and library for generating regular expressions from user-provided test cases
A command-line tool and library for generating regular expressions from user-provided test cases

Table of Contents What does this tool do? Do I still need to learn to write regexes then? Current features How to install? 4.1 The command-line tool 4

A morphological analysis library.

Lindera A Japanese morphological analysis library in Rust. This project fork from fulmicoton's kuromoji-rs. Lindera aims to build a library which is e

Wrapper around Microsoft CNTK library

Bindings for CNTK library Simple low level bindings for CNTK library from Microsoft. API Documentation Status Currently exploring ways how to interact

A lightweight library with vehicle tuning utilities.

A lightweight library with vehicle tuning utilities. This includes utilities for communicating with OBD-II services, firmware downloading/flashing, and table modifications.

lingua-rs Python binding. An accurate natural language detection library, suitable for long and short text alike.

lingua-py lingua-rs Python binding. An accurate natural language detection library, suitable for long and short text alike. Installation pip install l

Library to calculate TF-IDF (Term Frequency - Inverse Document Frequency) for generic documents.

Library to calculate TF-IDF (Term Frequency - Inverse Document Frequency) for generic documents. The library provides strategies to act on objects that implement certain document traits (NaiveDocument, ProcessedDocument, ExpandableDocument).

A simple and fast linear algebra library for games and graphics

glam A simple and fast 3D math library for games and graphics. Development status glam is in beta stage. Base functionality has been implemented and t

A rule based sentence segmentation library.

cutters A rule based sentence segmentation library. 🚧 This library is experimental. 🚧 Features Full UTF-8 support. Robust parsing. Language specific

nombytes is a library that provides a wrapper for the bytes::Bytes byte container for use with nom.

NomBytes nombytes is a library that provides a wrapper for the bytes::Bytes byte container for use with nom. I originally made this so that I could ha

Comments
  • Add newline ZST

    Add newline ZST

    #19 was mostly resolved with 2.0.0, but is missing the Newline separator.

    As I was not sure, if there was a reason for that, and I could use it, so I added it.

    What do you think?

    PS: #19 also mentions CharSpace, but I am not sure, what you mean by that.

    opened by mkroening 3
  • Collection model

    Collection model

    Rather than using cloneable iterators, create a new struct (deprecating Join) which will store the container itself, for collections that impl<'a, T> IntoIterator for &'a T

    opened by Lucretiel 1
  • Correctness bug in try_fold

    Correctness bug in try_fold

    https://github.com/Lucretiel/joinery/blob/ec33f1d099b4ebbe69ca13e1c7ac5dc75d23eeaa/src/lib.rs#L651-L661

    If try_fold exits early while processing a separator, the next element will be skipped, because it was passed to the folding function as an argument, but then never seen. This is probably insurmountable; at least, it is likely that there is no straightforward implementation of try_fold that users the underlying iterator's try_fold

    bug 
    opened by Lucretiel 0
  • Feature: zero-size or small separators for common use cases

    Feature: zero-size or small separators for common use cases

    Similar to NoSeparator, provide zero size or small types for common separator cases, including:

    • Newline
    • Space
    • Comma
    • CommaSpace
    • CharSpace (a char, followed by a space)
    • Dot
    • Dash
    • Underscore
    opened by Lucretiel 1
Owner
Nathan West
I make the sound that nerds make He/Him/His
Nathan West
An efficient and powerful Rust library for word wrapping text.

Textwrap Textwrap is a library for wrapping and indenting text. It is most often used by command-line programs to format dynamic output nicely so it l

Martin Geisler 322 Dec 26, 2022
⏮ ⏯ ⏭ A Rust library to easily read forwards, backwards or randomly through the lines of huge files.

EasyReader The main goal of this library is to allow long navigations through the lines of large files, freely moving forwards and backwards or gettin

Michele Federici 81 Dec 6, 2022
Natural language detection library for Rust. Try demo online: https://www.greyblake.com/whatlang/

Whatlang Natural language detection for Rust with focus on simplicity and performance. Content Features Get started Documentation Supported languages

Sergey Potapov 805 Dec 28, 2022
👄 The most accurate natural language detection library in the Rust ecosystem, suitable for long and short text alike

Table of Contents What does this library do? Why does this library exist? Which languages are supported? How good is it? Why is it better than other l

Peter M. Stahl 569 Jan 3, 2023
A fast, low-resource Natural Language Processing and Text Correction library written in Rust.

nlprule A fast, low-resource Natural Language Processing and Error Correction library written in Rust. nlprule implements a rule- and lookup-based app

Benjamin Minixhofer 496 Jan 8, 2023
Rust wrapper for the BlingFire tokenization library

BlingFire in Rust blingfire is a thin Rust wrapper for the BlingFire tokenization library. Add the library to Cargo.toml to get started cargo add blin

Re:infer 14 Sep 5, 2022
A Rust library containing an offline version of webster's dictionary.

webster-rs A Rust library containing an offline version of webster's dictionary. Add to Cargo.toml webster = 0.3.0 Simple example: fn main() { le

Grant Handy 12 Sep 27, 2022
A small random number generator hacked on top of Rust's standard library. An exercise in pointlessness.

attorand from 'atto', meaning smaller than small, and 'rand', short for random. A small random number generator hacked on top of Rust's standard libra

Isaac Clayton 1 Nov 24, 2021
A small rust library for creating regex-based lexers

A small rust library for creating regex-based lexers

nph 1 Feb 5, 2022
Implementation of sentence embeddings with BERT in Rust, using the Burn library.

Sentence Transformers in Burn This library provides an implementation of the Sentence Transformers framework for computing text representations as vec

Tyler Vergho 4 Sep 4, 2023