Skip to content

only require Show for elements joined with connect #13911

Closed
@panzi

Description

@panzi

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(", "));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions