5

Currently the SliceConcatExt seems to be very specifically crafted for slices or vectors of Strings, even though it arbitrarily constrains its use. That particular use-case is reflected in the trait name as well, after all, it is called SliceConcatExt for a reason.

Is there a more general connect() implementation which would take any Iterator over items supporting the Str trait ?. If not, are there any plans to remedy this ?

Example

use std::iter::IntoIterator;

fn connected<S, I>(s: I) -> String
where S: Str,
      I: IntoIterator<Item=S> {
    // have
    s.into_iter().collect::<Vec<S>>().connect(", ")

    // want
    // s.into_iter().connect(", ")
    // error: type `<I as core::iter::IntoIterator>::IntoIter` does not implement any method in scope named `connect`
    // tests/lang.rs:790         s.into_iter().connect(", ")
}

connected(&["foo", "bar"]);

One could possibly implement SliceConcatExt for any iterator with item type Str, but I have the suspicion that connect() currently is just unnecessarily specialized, which might be fixable until Rust beta.

Using rustc 1.0.0-nightly (522d09dfe 2015-02-19) (built 2015-02-19)

Byron
  • 3,908
  • 3
  • 26
  • 35
  • Shouldn't this be posted on the rust bug tracker instead ? – Levans Feb 23 '15 at 11:40
  • I am not sure if it is intentional or not, as I always have to assume complete err on my side. If we conclude it is more like an API design issue, it should move on to the bug tracker indeed. – Byron Feb 23 '15 at 11:42
  • By the way, if anyone is indeed creating an issue on github about this, please CC me using @Byron. Thank you – Byron Feb 23 '15 at 12:07
  • Seems like it would be implementable for `IntoIterator`, yes. Maybe less efficiently, but implementable. I would encourage you to search the bug tracker for an issue on this, or open a new one – Renato Zannon Feb 23 '15 at 12:21
  • I have created a new issue: https://github.com/rust-lang/rust/issues/22754 – Byron Feb 24 '15 at 07:06

1 Answers1

4

The closest solution I know of would be to use Itertools::intersperse:

#![feature(core)]

extern crate itertools;

use std::iter::IntoIterator;
use itertools::Itertools;

fn connected<'a, S, I>(s: I) -> String //'
    where S: Str,
          I: IntoIterator<Item=&'a S> //'
{
    s.into_iter().map(|s| s.as_slice()).intersperse(", ").collect()
}

fn main() {
    println!("{}", connected(&["foo", "bar"]));
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • ah you got it working. Good to see where I got stuck. – oli_obk Feb 23 '15 at 14:46
  • That will do ! Thank you. I still think it's a usability issue, either solved by improving the docs, or ... just making the common string case easier (or more obvious) to use. – Byron Feb 23 '15 at 15:12
  • And also, isn't the livetime magic happening at `I: IntoIterator` unnecessary for inputs like 'static strings ? If I remove it, and pass in a static str, it says it doesn't live long enough. Also I'd think that `S: Str + 'a` is the same, but more general, but it's not the case. Maybe something for another question though. – Byron Feb 23 '15 at 16:45