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

only require Show for elements joined with connect #13911

Closed
panzi opened this issue May 3, 2014 · 5 comments
Closed

only require Show for elements joined with connect #13911

panzi opened this issue May 3, 2014 · 5 comments

Comments

@panzi
Copy link

panzi commented May 3, 2014

In most high level languages it's easy to turn a list/array of things into a comma separated string. In Rust there is the connect method, but it requires the elements to implement the trait std::str::Str. I would like this method to require std::fmt::Show instead. Alternatively a join method with according semantics that requires std::fmt::Show would suffice.

Currently you have to write:

let xs = [1, 2, 3];
let str_xs: ~[~str] = xs.iter().map(|x| x.to_str()).collect();
println!("xs = {}", str_xs.connect(", "));

let mut ys = Vec::new();
ys.push(4);
ys.push(5);
ys.push(6);
let str_ys: Vec<~str> = ys.iter().map(|y| y.to_str()).collect();
println!("ys = {}", str_ys.connect(", "));

This is pretty horrible. This would be much nicer:

let xs = [1, 2, 3];
println!("xs = {}", xs.connect(", "));

let mut ys = Vec::new();
ys.push(4);
ys.push(5);
ys.push(6);
println!("ys = {}", ys.connect(", "));
@ghost
Copy link

ghost commented May 3, 2014

Would it be a better idea to define connect() on iterators of strings instead? It would then just be:

xs.iter().map(|x| x.to_str()).connect(", ")

but I think that explicit step is useful. It doesn't feel right for the formatting step to happen implicitly if there's no way to pass in a custom formatter.

@huonw
Copy link
Member

huonw commented May 3, 2014

Theoretically we can have low-alloc connection by using Show directly (using .to_str like that will mean a ~str gets allocated for every element).

The most general form would be something like

fn connect_into<W: Writer, S: Show, It: Iterator<S>>(w: &mut W, it: It, connector: &str) {
    for (i, x) in it.enumerate() {
       if i == 0 {
           write!(w, "{}", x);
       } else {
            write!(w, "{}{}", connector, x)
       }
    }
}

connect_into(&mut std::io::stdout(), range(0, 3), ", "); // prints: 0, 1, 2

(I guess you could even have the connector being generic too.)

@hackervera
Copy link

+1 for having connect for a Show or something simple like that

@Swatinem
Copy link
Contributor

Yes. It should definitely be defined for Iterator<Show>.

@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. This method is now stable, and so would need to go through an RFC to change.

This issue has been moved to the RFCs repo: rust-lang/rfcs#720

bors added a commit to rust-lang-ci/rust that referenced this issue Feb 13, 2023
flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 9, 2025
… comments (rust-lang#13911)

Fixes rust-lang#13692.

If the `vec!` macro call contains comments, we should not provide
suggestions and let users handle it however they see fit.

changelog: Only emit `useless_vec` suggestion if the macro does not
contain code comments
flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 9, 2025
…mments (rust-lang#13940)

Fixes rust-lang/rust-clippy#8528.

Similar to rust-lang#13911, if there are code comments, we don't want to remove
them automatically.

changelog: Don't emit machine applicable `map_flatten` lint if there are
code comments

r? @xFrednet
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

5 participants