[rust-dev] Preserving formatting for slice's Show impl

Tom Jakubowski tom at crystae.net
Mon Jun 9 20:50:33 PDT 2014


I would expect that `println!("{:_>4}", [1].as_slice());` would print either `[___1]` (where the format is "mapped" over the slice) or `_[1]` (where the format is applied to the slice as a whole), but instead no formatting is applied at all and it simply prints `[1]`. 

I can see uses and arguments for both the "mapping" and "whole” interpretations of the format string on slices. On the one hand this ambiguity makes a case for leaving the behavior as-is for backwards compatibility. On the other hand it would be useful to be able to format slices (and other collections, of course). Would it be appropriate to expand the syntax for format strings to allow for nested format strings, so that separate formatting can be applied to the entire collection and to its contents? I assume it this would require an RFC.

(The "mapped" variant can be very easily implemented, by the way, by replacing `try!(write!("{}", x))` with `try!(x.fmt(f))` in the `impl<T: Show> Show for &[T]`.)

Tom

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140609/85b430c0/attachment.html>


More information about the Rust-dev mailing list