@@ -34,12 +34,12 @@ arguments directly while performing minimal allocations.
3434Some examples of the `format!` extension are:
3535
3636```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"
4343```
4444
4545From 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
6262iterator advances. This leads to behavior like this:
6363
6464```rust
65- format!("{1} {} {0} {}", 1, 2) // => ~"2 1 1 2"
65+ format!("{1} {} {0} {}", 1, 2); // => ~"2 1 1 2"
6666```
6767
6868The internal iterator over the argument has not been advanced by the time the
@@ -89,9 +89,9 @@ identifier '=' expression
8989For example, the following `format!` expressions all use named argument:
9090
9191```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 ()"
9595```
9696
9797It 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
160160method of the signature:
161161
162162```rust
163+ # use std;
164+ # struct T;
165+ # trait SomeName<T> {
163166fn fmt(value: &T, f: &mut std::fmt::Formatter);
167+ # }
164168```
165169
166170Your type will be passed by-reference in `value`, and then the function should
@@ -218,7 +222,7 @@ fn main() {
218222There are a number of related macros in the `format!` family. The ones that are
219223currently implemented are:
220224
221- ```rust
225+ ```rust,notest
222226format! // described above
223227write! // first argument is a &mut io::Writer, the destination
224228writeln! // same as write but appends a newline
@@ -261,9 +265,13 @@ references information on the stack. Under the hood, all of
261265the related macros are implemented in terms of this. First
262266off, some example usage is:
263267
264- ```rust
268+ ```rust,ignore
265269use std::fmt;
266270
271+ # fn lol<T>() -> T { fail!() }
272+ # let my_writer: &mut ::std::io::Writer = lol();
273+ # let my_fn: fn(&fmt::Arguments) = lol();
274+
267275format_args!(fmt::format, "this returns {}", "~str");
268276format_args!(|args| { fmt::write(my_writer, args) }, "some {}", "args");
269277format_args!(my_fn, "format {}", "string");
@@ -305,7 +313,7 @@ to reference the string value of the argument which was selected upon. As an
305313example:
306314
307315```rust
308- format!("{0, select, other{#}}", "hello") // => ~"hello"
316+ format!("{0, select, other{#}}", "hello"); // => ~"hello"
309317```
310318
311319This example is the equivalent of `{0:s}` essentially.
@@ -585,7 +593,9 @@ pub trait Float { fn fmt(&Self, &mut Formatter); }
585593///
586594/// ```rust
587595/// use std::fmt;
588- /// let w: &mut io::Writer = ...;
596+ /// use std::io;
597+ ///
598+ /// let w = &mut io::stdout() as &mut io::Writer;
589599/// format_args!(|args| { fmt::write(w, args) }, "Hello, {}!", "world");
590600/// ```
591601pub fn write ( output : & mut io:: Writer , args : & Arguments ) {
@@ -650,8 +660,9 @@ pub unsafe fn write_unsafe(output: &mut io::Writer,
650660///
651661/// ```rust
652662/// use std::fmt;
663+ ///
653664/// let s = format_args!(fmt::format, "Hello, {}!", "world");
654- /// assert_eq!(s, "Hello, world!");
665+ /// assert_eq!(s, ~ "Hello, world!");
655666/// ```
656667pub fn format ( args : & Arguments ) -> ~str {
657668 unsafe { format_unsafe ( args. fmt , args. args ) }
0 commit comments