Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add join(prefix, sep, suffix) -> String #981

Open
ronanM opened this issue Aug 6, 2024 · 4 comments
Open

Add join(prefix, sep, suffix) -> String #981

ronanM opened this issue Aug 6, 2024 · 4 comments

Comments

@ronanM
Copy link

ronanM commented Aug 6, 2024

Add a join() operator with prefix, sep and suffix.

assert_eq!([1, 2, 3].iter().join("<[", ", ", "]>"), "<[1, 2, 3]>");

@jswrenn
Copy link
Member

jswrenn commented Aug 6, 2024

So this:

[1, 2, 3].iter().join("<[", ", ", "]>")

...would be a shorthand for this:

format!("<[{}]>", [1, 2, 3].iter().format(", "))

@phimuemue
Copy link
Member

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.

@jswrenn
Copy link
Member

jswrenn commented Aug 6, 2024

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.

@scottmcm
Copy link
Contributor

scottmcm commented Aug 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants