Skip to content

Commit 7a8324d

Browse files
committed
Document traits and Default about format! better
Closes #9865 Closes #9808
1 parent a1848bc commit 7a8324d

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/libstd/fmt/mod.rs

+48-3
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,14 @@ The current mapping of types to traits is:
147147
* `p` => Pointer
148148
* `t` => Binary
149149
* `f` => Float
150+
* `` (nothing) => Default
150151
151152
What this means is that any type of argument which implements the
152153
`std::fmt::Binary` trait can then be formatted with `{:t}`. Implementations are
153154
provided for these traits for a number of primitive types by the standard
154-
library as well. Again, the default formatting type (if no other is specified)
155-
is `?` which is defined for all types by default.
155+
library as well. If no format is specified (as in `{}` or `{:6}`), then the
156+
format trait used is the `Default` trait. This is one of the more commonly
157+
implemented traits when formatting a custom type.
156158
157159
When implementing a format trait for your own time, you will have to implement a
158160
method of the signature:
@@ -166,7 +168,50 @@ emit output into the `f.buf` stream. It is up to each format trait
166168
implementation to correctly adhere to the requested formatting parameters. The
167169
values of these parameters will be listed in the fields of the `Formatter`
168170
struct. In order to help with this, the `Formatter` struct also provides some
169-
helper methods.
171+
helper methods. An example of implementing the formatting traits would look
172+
like:
173+
174+
```rust
175+
use std::fmt;
176+
use std::f64;
177+
178+
struct Vector2D {
179+
x: int,
180+
y: int,
181+
}
182+
183+
impl fmt::Default for Vector2D {
184+
fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) {
185+
// The `f.buf` value is of the type `&mut io::Writer`, which is what th
186+
// write! macro is expecting. Note that this formatting ignores the
187+
// various flags provided to format strings.
188+
write!(f.buf, "({}, {})", obj.x, obj.y)
189+
}
190+
}
191+
192+
// Different traits allow different forms of output of a type. The meaning of
193+
// this format is to print the magnitude of a vector.
194+
impl fmt::Binary for Vector2D {
195+
fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) {
196+
let magnitude = (obj.x * obj.x + obj.y * obj.y) as f64;
197+
let magnitude = magnitude.sqrt();
198+
199+
// Respect the formatting flags by using the helper method
200+
// `pad_integral` on the Formatter object. See the method documentation
201+
// for details, and the function `pad` can be used to pad strings.
202+
let decimals = f.precision.unwrap_or(3);
203+
let string = f64::to_str_exact(magnitude, decimals);
204+
f.pad_integral(string.as_bytes(), "", true);
205+
}
206+
}
207+
208+
fn main() {
209+
let myvector = Vector2D { x: 3, y: 4 };
210+
211+
println!("{}", myvector); // => "(3, 4)"
212+
println!("{:10.3t}", myvector); // => " 5.000"
213+
}
214+
```
170215
171216
### Related macros
172217

0 commit comments

Comments
 (0)