@@ -133,7 +133,7 @@ is `?` which is defined for all types by default.
133
133
When implementing a format trait for your own time, you will have to implement a
134
134
method of the signature:
135
135
136
- ~~~
136
+ ~~~{.rust}
137
137
fn fmt(value: &T, f: &mut std::fmt::Formatter);
138
138
~~~
139
139
@@ -144,6 +144,78 @@ values of these parameters will be listed in the fields of the `Formatter`
144
144
struct. In order to help with this, the `Formatter` struct also provides some
145
145
helper methods.
146
146
147
+ ### Related macros
148
+
149
+ There are a number of related macros in the `format!` family. The ones that are
150
+ currently implemented are:
151
+
152
+ ~~~{.rust}
153
+ format! // described above
154
+ write! // first argument is a &mut rt::io::Writer, the destination
155
+ writeln! // same as write but appends a newline
156
+ print! // the format string is printed to the standard output
157
+ println! // same as print but appends a newline
158
+ format_args! // described below.
159
+ ~~~
160
+
161
+
162
+ #### `write!`
163
+
164
+ This and `writeln` are two macros which are used to emit the format string to a
165
+ specified stream. This is used to prevent intermediate allocations of format
166
+ strings and instead directly write the output. Under the hood, this function is
167
+ actually invoking the `write` function defined in this module. Example usage is:
168
+
169
+ ~~~{.rust}
170
+ use std::rt::io;
171
+
172
+ let mut w = io::mem::MemWriter::new();
173
+ write!(&mut w as &mut io::Writer, "Hello {}!", "world");
174
+ ~~~
175
+
176
+ #### `print!`
177
+
178
+ This and `println` emit their output to stdout. Similarly to the `write!` macro,
179
+ the goal of these macros is to avoid intermediate allocations when printing
180
+ output. Example usage is:
181
+
182
+ ~~~{.rust}
183
+ print!("Hello {}!", "world");
184
+ println!("I have a newline {}", "character at the end");
185
+ ~~~
186
+
187
+ #### `format_args!`
188
+ This is a curious macro which is used to safely pass around
189
+ an opaque object describing the format string. This object
190
+ does not require any heap allocations to create, and it only
191
+ references information on the stack. Under the hood, all of
192
+ the related macros are implemented in terms of this. First
193
+ off, some example usage is:
194
+
195
+ ~~~{.rust}
196
+ use std::fmt;
197
+
198
+ format_args!(fmt::format, "this returns {}", "~str");
199
+ format_args!(|args| { fmt::write(my_writer, args) }, "some {}", "args");
200
+ format_args!(my_fn, "format {}", "string");
201
+ ~~~
202
+
203
+ The first argument of the `format_args!` macro is a function (or closure) which
204
+ takes one argument of type `&fmt::Arguments`. This structure can then be
205
+ passed to the `write` and `format` functions inside this module in order to
206
+ process the format string. The goal of this macro is to even further prevent
207
+ intermediate allocations when dealing formatting strings.
208
+
209
+ For example, a logging library could use the standard formatting syntax, but it
210
+ would internally pass around this structure until it has been determined where
211
+ output should go to.
212
+
213
+ It is unsafe to programmatically create an instance of `fmt::Arguments` because
214
+ the operations performed when executing a format string require the compile-time
215
+ checks provided by the compiler. The `format_args!` macro is the only method of
216
+ safely creating these structures, but they can be unsafely created with the
217
+ constructor provided.
218
+
147
219
## Internationalization
148
220
149
221
The formatting syntax supported by the `format!` extension supports
@@ -163,7 +235,7 @@ Furthermore, whenever a case is running, the special character `#` can be used
163
235
to reference the string value of the argument which was selected upon. As an
164
236
example:
165
237
166
- ~~~
238
+ ~~~{.rust}
167
239
format!("{0, select, other{#}}", "hello") // => ~"hello"
168
240
~~~
169
241
0 commit comments