itertools
itertools copied to clipboard
Add `join(prefix, sep, suffix) -> String`
Add a join() operator with prefix, sep and suffix.
assert_eq!([1, 2, 3].iter().join("<[", ", ", "]>"), "<[1, 2, 3]>");
So this:
[1, 2, 3].iter().join("<[", ", ", "]>")
...would be a shorthand for this:
format!("<[{}]>", [1, 2, 3].iter().format(", "))
I‘m pretty sure that - if we do this - we should aim for interleave_with_prefix_separator_suffix, ie. not tie it to strings.
Users then can still call join manually if they want.
I know it may be a bit annoying if prefix/separator/suffix have different types, but the concept seems not inherently related to text.
So
iter.foo(start, sep, end)
...would be a shorthand for:
once(start).chain(iter.intersperse(sep)).chain(once(end))
That's quite a mouthful, and I think the argument for providing a shorthand for that is a bit stronger. Then again, I cannot think of a time where I've needed such a pattern outside of string formatting, and this shorthand works poorly for string formatting when the item type isn't a string.
This makes me think of this conversation about Display adapters: https://users.rust-lang.org/t/joining-numbers-into-a-string/115540/7?u=scottmcm