Skip to content

Commit fd8c05c

Browse files
committed
Document all of the format! related macros
1 parent cfe3db8 commit fd8c05c

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/libstd/fmt/mod.rs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ is `?` which is defined for all types by default.
133133
When implementing a format trait for your own time, you will have to implement a
134134
method of the signature:
135135
136-
~~~
136+
~~~{.rust}
137137
fn fmt(value: &T, f: &mut std::fmt::Formatter);
138138
~~~
139139
@@ -144,6 +144,78 @@ values of these parameters will be listed in the fields of the `Formatter`
144144
struct. In order to help with this, the `Formatter` struct also provides some
145145
helper methods.
146146
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+
147219
## Internationalization
148220
149221
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
163235
to reference the string value of the argument which was selected upon. As an
164236
example:
165237
166-
~~~
238+
~~~{.rust}
167239
format!("{0, select, other{#}}", "hello") // => ~"hello"
168240
~~~
169241

src/test/compile-fail/ifmt-bad-arg.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ fn main() {
7272
format!("foo } bar"); //~ ERROR: unmatched `}` found
7373
format!("foo }"); //~ ERROR: unmatched `}` found
7474

75-
// FIXME(#5794) the spans on these errors are pretty terrible
76-
//format!();
77-
//format!("" 1);
78-
//format!("", 1 1);
75+
format!(); //~ ERROR: requires at least a format string argument
76+
format!("" 1); //~ ERROR: expected token: `,`
77+
format!("", 1 1); //~ ERROR: expected token: `,`
7978
}

0 commit comments

Comments
 (0)