Trait compact_str::CompactStringExt

source ·
pub trait CompactStringExt {
    // Required methods
    fn concat_compact(&self) -> CompactString;
    fn join_compact<S: AsRef<str>>(&self, separator: S) -> CompactString;
}
Expand description

A trait that provides convenience methods for creating a CompactString from a collection of items. It is implemented for all types that can be converted into an iterator, and that iterator yields types that can be converted into a str.

i.e. C: IntoIterator<Item = AsRef<str>>.

§Concatenate and Join

Two methods that this trait provides are concat_compact(...) and join_compact(...)

use compact_str::CompactStringExt;

let words = vec!["☀️", "🌕", "🌑", "☀️"];

// directly concatenate all the words together
let concat = words.concat_compact();
assert_eq!(concat, "☀️🌕🌑☀️");

// join the words, with a separator
let join = words.join_compact(" ➡️ ");
assert_eq!(join, "☀️ ➡️ 🌕 ➡️ 🌑 ➡️ ☀️");

Required Methods§

source

fn concat_compact(&self) -> CompactString

Concatenates all the items of a collection into a CompactString

§Example
use compact_str::CompactStringExt;

let items = ["hello", " ", "world", "!"];
let compact = items.concat_compact();

assert_eq!(compact, "hello world!");
source

fn join_compact<S: AsRef<str>>(&self, separator: S) -> CompactString

Joins all the items of a collection, placing a separator between them, forming a CompactString

§Example
use compact_str::CompactStringExt;

let fruits = vec!["apples", "oranges", "bananas"];
let compact = fruits.join_compact(", ");

assert_eq!(compact, "apples, oranges, bananas");

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<I, C> CompactStringExt for C
where I: AsRef<str>, for<'a> &'a C: IntoIterator<Item = &'a I>,