@@ -34,12 +34,12 @@ arguments directly while performing minimal allocations.
34
34
Some examples of the `format!` extension are:
35
35
36
36
```rust
37
- format!("Hello") // => ~"Hello"
38
- format!("Hello, {:s}!", "world") // => ~"Hello, world!"
39
- format!("The number is {:d}", 1) // => ~"The number is 1"
40
- format!("{:?}", ~[3, 4]) // => ~"~[3, 4]"
41
- format!("{value}", value=4) // => ~"4"
42
- format!("{} {}", 1, 2) // => ~"1 2"
37
+ format!("Hello"); // => ~"Hello"
38
+ format!("Hello, {:s}!", "world"); // => ~"Hello, world!"
39
+ format!("The number is {:d}", 1); // => ~"The number is 1"
40
+ format!("{:?}", ~[3, 4]); // => ~"~[3, 4]"
41
+ format!("{value}", value=4); // => ~"4"
42
+ format!("{} {}", 1, 2); // => ~"1 2"
43
43
```
44
44
45
45
From these, you can see that the first argument is a format string. It is
@@ -62,7 +62,7 @@ iterator over the argument. Each time a "next argument" specifier is seen, the
62
62
iterator advances. This leads to behavior like this:
63
63
64
64
```rust
65
- format!("{1} {} {0} {}", 1, 2) // => ~"2 1 1 2"
65
+ format!("{1} {} {0} {}", 1, 2); // => ~"2 1 1 2"
66
66
```
67
67
68
68
The internal iterator over the argument has not been advanced by the time the
@@ -89,9 +89,9 @@ identifier '=' expression
89
89
For example, the following `format!` expressions all use named argument:
90
90
91
91
```rust
92
- format!("{argument}", argument = "test") // => ~"test"
93
- format!("{name} {}", 1, name = 2) // => ~"2 1"
94
- format!("{a:s} {c:d} {b:?}", a="a", b=(), c=3) // => ~"a 3 ()"
92
+ format!("{argument}", argument = "test"); // => ~"test"
93
+ format!("{name} {}", 1, name = 2); // => ~"2 1"
94
+ format!("{a:s} {c:d} {b:?}", a="a", b=(), c=3); // => ~"a 3 ()"
95
95
```
96
96
97
97
It is illegal to put positional parameters (those without names) after arguments
@@ -160,7 +160,11 @@ When implementing a format trait for your own time, you will have to implement a
160
160
method of the signature:
161
161
162
162
```rust
163
+ # use std;
164
+ # struct T;
165
+ # trait SomeName<T> {
163
166
fn fmt(value: &T, f: &mut std::fmt::Formatter);
167
+ # }
164
168
```
165
169
166
170
Your type will be passed by-reference in `value`, and then the function should
@@ -218,7 +222,7 @@ fn main() {
218
222
There are a number of related macros in the `format!` family. The ones that are
219
223
currently implemented are:
220
224
221
- ```rust
225
+ ```rust,notest
222
226
format! // described above
223
227
write! // first argument is a &mut io::Writer, the destination
224
228
writeln! // same as write but appends a newline
@@ -261,9 +265,13 @@ references information on the stack. Under the hood, all of
261
265
the related macros are implemented in terms of this. First
262
266
off, some example usage is:
263
267
264
- ```rust
268
+ ```rust,ignore
265
269
use std::fmt;
266
270
271
+ # fn lol<T>() -> T { fail!() }
272
+ # let my_writer: &mut ::std::io::Writer = lol();
273
+ # let my_fn: fn(&fmt::Arguments) = lol();
274
+
267
275
format_args!(fmt::format, "this returns {}", "~str");
268
276
format_args!(|args| { fmt::write(my_writer, args) }, "some {}", "args");
269
277
format_args!(my_fn, "format {}", "string");
@@ -305,7 +313,7 @@ to reference the string value of the argument which was selected upon. As an
305
313
example:
306
314
307
315
```rust
308
- format!("{0, select, other{#}}", "hello") // => ~"hello"
316
+ format!("{0, select, other{#}}", "hello"); // => ~"hello"
309
317
```
310
318
311
319
This example is the equivalent of `{0:s}` essentially.
@@ -585,7 +593,9 @@ pub trait Float { fn fmt(&Self, &mut Formatter); }
585
593
///
586
594
/// ```rust
587
595
/// use std::fmt;
588
- /// let w: &mut io::Writer = ...;
596
+ /// use std::io;
597
+ ///
598
+ /// let w = &mut io::stdout() as &mut io::Writer;
589
599
/// format_args!(|args| { fmt::write(w, args) }, "Hello, {}!", "world");
590
600
/// ```
591
601
pub fn write ( output : & mut io:: Writer , args : & Arguments ) {
@@ -650,8 +660,9 @@ pub unsafe fn write_unsafe(output: &mut io::Writer,
650
660
///
651
661
/// ```rust
652
662
/// use std::fmt;
663
+ ///
653
664
/// let s = format_args!(fmt::format, "Hello, {}!", "world");
654
- /// assert_eq!(s, "Hello, world!");
665
+ /// assert_eq!(s, ~ "Hello, world!");
655
666
/// ```
656
667
pub fn format ( args : & Arguments ) -> ~str {
657
668
unsafe { format_unsafe ( args. fmt , args. args ) }
0 commit comments