somethimes I wish Rust had repr formatter, similar to Python repr function.
println!("{:?}", "\n");
should print
"\n"
instead of newline.
Repr can be used, for instance, in assert_eq! macro instead of {}, which prints something inappropriate in cases like this:
assert_eq!("\n", "\r");
ShowRepr trait is used instead of Show for {:?} formatter.
IMO, Show for Option or Vec should use Repr to for its content, like this:
impl<T : ShowRepr> Show for Option<T> {
fn fmt(...) {
match self {
// ShowRepr is used instead of Show
Some(v) => format!("Some({:?})", v),
None => format!("None"),
}
}
}
I’m quite interested in this as well. I have half an RFC written up, I’m just trying to figure out how the default case should be. I assume anything Show should automatically be able to be Repr. However, we can’t do impl<T: Show> Repr for T, because then nothing can have specialized Repr vs Show.
The one solution I think works well is to, instead of adding a new trait, add a new method to Show, that has a default implementation. This way, all Show can be repred, and anything that wants to specialize can override the default method.
It would be nice if this would be using a more intelligent formatter that can indent and wrap. Something like ruby’s pprint system or Python’s pretty library.
The python repr that is (almost) always is single line is nice. This means that it’s safe to put in logs or print to console, even if value is something that is got from third party.
For example if you are printing error value:
print("Server returned bad response: ", repr(e))
It’s guaranteed that it will never print:
Server returned bad response:
[ ... 1000 newlines ... ]
Something wrong happened, but it can be fixed if you run:
sudo rm -rf /
I’m not saying it should be indented by default. The fact that the repr system in Python does not support optional indentation has been a major problem for a long time now and it’s impossible to fix now. But a new language should not make the same mistake.