@@ -106,35 +106,31 @@ named parameters that are unused by the format string.
106
106
107
107
### Argument types
108
108
109
- Each argument's type is dictated by the format string. It is a requirement that
110
- every argument is only ever referred to by one type. For example, this is an
111
- invalid format string:
109
+ Each argument's type is dictated by the format string. It is a requirement that every argument is
110
+ only ever referred to by one type. For example, this is an invalid format string:
112
111
113
112
```text
114
- {0:d } {0:s }
113
+ {0:x } {0:o }
115
114
```
116
115
117
- This is invalid because the first argument is both referred to as an integer as
118
- well as a string .
116
+ This is invalid because the first argument is both referred to as a hexidecimal as well as an
117
+ octal .
119
118
120
- Because formatting is done via traits, there is no requirement that the
121
- `d` format actually takes an `int`, but rather it simply requires a type which
122
- ascribes to the `Signed` formatting trait. There are various parameters which do
123
- require a particular type, however. Namely if the syntax `{:.*s}` is used, then
124
- the number of characters to print from the string precedes the actual string and
125
- must have the type `uint`. Although a `uint` can be printed with `{:u}`, it is
126
- illegal to reference an argument as such. For example, this is another invalid
119
+ There are various parameters which do require a particular type, however. Namely if the syntax
120
+ `{:.*}` is used, then the number of characters to print precedes the actual object being formatted,
121
+ and the number of characters must have the type `uint`. Although a `uint` can be printed with
122
+ `{}`, it is illegal to reference an argument as such. For example this is another invalid
127
123
format string:
128
124
129
125
```text
130
- {:.*s } {0:u }
126
+ {:.*} {0}
131
127
```
132
128
133
129
### Formatting traits
134
130
135
131
When requesting that an argument be formatted with a particular type, you are
136
132
actually requesting that an argument ascribes to a particular trait. This allows
137
- multiple actual types to be formatted via `{:d }` (like `i8` as well as `int`).
133
+ multiple actual types to be formatted via `{:x }` (like `i8` as well as `int`).
138
134
The current mapping of types to traits is:
139
135
140
136
* *nothing* ⇒ `Show`
@@ -157,12 +153,12 @@ When implementing a format trait for your own type, you will have to implement a
157
153
method of the signature:
158
154
159
155
```rust
160
- # use std;
161
- # mod fmt { pub type Result = (); }
162
- # struct T;
163
- # trait SomeName<T> {
164
- fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result;
165
- # }
156
+ # use std::fmt ;
157
+ # struct Foo; // our custom type
158
+ # impl fmt::Show for Foo {
159
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
160
+ # write!(f, "testing, testing")
161
+ # } }
166
162
```
167
163
168
164
Your type will be passed as `self` by-reference, and then the function should
@@ -237,7 +233,6 @@ println! // same as print but appends a newline
237
233
format_args! // described below.
238
234
```
239
235
240
-
241
236
#### `write!`
242
237
243
238
This and `writeln` are two macros which are used to emit the format string to a
0 commit comments