-
Notifications
You must be signed in to change notification settings - Fork 313
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
Itertools::join #526
Comments
How about something like: trait JoinableIterator<S>: Iterator {
fn join<O>(self, sep: S) -> O
where
Self: Sized,
O: Join<Self::Item, S>
{
O::join(self, sep)
}
}
impl<I: Iterator, S> JoinableIterator<S> for I {}
trait Join<T, S> {
fn join<I: IntoIterator<Item = T>>(iter: I, sep: S) -> Self;
}
impl<T: std::fmt::Display, S: std::fmt::Display> Join<T, S> for String {
fn join<I: IntoIterator<Item = T>>(iter: I, sep: S) -> Self {
let mut iter = iter.into_iter();
match iter.next() {
Some(item) => {
use std::fmt::Write;
let mut s = item.to_string();
iter.for_each(|item| {
let _ = write!(s, "{}", sep);
let _ = write!(s, "{}", item);
});
s
}
None => String::new(),
}
}
}
// Possibly other implementations The |
I thought about something like that but I haven't been able to figure it out. But maybe not |
I based myself on the current implementation which requires As for the |
User could still use
No, user can override that. Still, that is the reason why I think it is weird and didn't end up using this join from itertools. Also, if we use |
You still need to get that
On stable? I don't think so. The following code doesn't compile if you don't enable specialization (however with specialization it does) struct Foo;
impl std::fmt::Display for Foo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
impl std::string::ToString for Foo {
fn to_string(&self) -> String {
todo!()
}
}
|
Yes, they have to convert between different types. On the other hand, what if there is a secret type than the users does not want to implement I do think Or maybe we should ask in standard library why didn't they do Say, if users have |
This is a pretty good point. Maybe alternatively we could have a |
Good idea, or maybe we can have a function but I wonder if we could let users specify their own trait bounds? Do we give the users power to do By the way, happy chinese new year! |
Our current
join
only outputsString
, maybe we should consider using the nightlyJoin
in standard library? I created a pull request using that in standard library but I noticed requires allocation so it is less likely to be included in standardstd
library.rust-lang/rust#75738
Maybe it can be considered here after it is stable on standard library?
The text was updated successfully, but these errors were encountered: