This is my first try using Rust. I just tried to pretty-print a vector. Overall experience wasn't that bad, I actually dig the fact that I can expand the library types with custom methods. My only concern is if someday my custom methods collide with the library methods.
Well, here's my code, what do you guys think about it?
trait Methods {
fn append(&mut self, &[&str]) -> &Self;
fn replace_last(&mut self, &str) -> &Self;
}
impl Methods for String {
fn append(&mut self, input: &[&str]) -> &Self {
for e in input {
self.push_str(e);
}
self
}
fn replace_last(&mut self, input: &str) -> &Self {
self.pop();
self.push_str(input);
self
}
}
fn to_string(input: &[&str]) -> String {
let mut output = String::from("(");
for e in input {
output.append(&[e, " "]);
}
output.replace_last(")");
output
}
fn main() {
let x = vec!["A", "B", "C"];
println!("{}", to_string(&x));
}