parent
48ca6d1840
commit
7dadb14fb2
1 changed files with 17 additions and 22 deletions
|
@ -106,35 +106,31 @@ named parameters that are unused by the format string.
|
||||||
|
|
||||||
### Argument types
|
### Argument types
|
||||||
|
|
||||||
Each argument's type is dictated by the format string. It is a requirement that
|
Each argument's type is dictated by the format string. It is a requirement that every argument is
|
||||||
every argument is only ever referred to by one type. For example, this is an
|
only ever referred to by one type. For example, this is an invalid format string:
|
||||||
invalid format string:
|
|
||||||
|
|
||||||
```text
|
```text
|
||||||
{0:d} {0:s}
|
{0:x} {0:o}
|
||||||
```
|
```
|
||||||
|
|
||||||
This is invalid because the first argument is both referred to as an integer as
|
This is invalid because the first argument is both referred to as a hexidecimal as well as an
|
||||||
well as a string.
|
octal.
|
||||||
|
|
||||||
Because formatting is done via traits, there is no requirement that the
|
There are various parameters which do require a particular type, however. Namely if the syntax
|
||||||
`d` format actually takes an `int`, but rather it simply requires a type which
|
`{:.*}` is used, then the number of characters to print precedes the actual object being formatted,
|
||||||
ascribes to the `Signed` formatting trait. There are various parameters which do
|
and the number of characters must have the type `uint`. Although a `uint` can be printed with
|
||||||
require a particular type, however. Namely if the syntax `{:.*s}` is used, then
|
`{}`, it is illegal to reference an argument as such. For example this is another invalid
|
||||||
the number of characters to print from the string precedes the actual string and
|
|
||||||
must have the type `uint`. Although a `uint` can be printed with `{:u}`, it is
|
|
||||||
illegal to reference an argument as such. For example, this is another invalid
|
|
||||||
format string:
|
format string:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
{:.*s} {0:u}
|
{:.*} {0}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Formatting traits
|
### Formatting traits
|
||||||
|
|
||||||
When requesting that an argument be formatted with a particular type, you are
|
When requesting that an argument be formatted with a particular type, you are
|
||||||
actually requesting that an argument ascribes to a particular trait. This allows
|
actually requesting that an argument ascribes to a particular trait. This allows
|
||||||
multiple actual types to be formatted via `{:d}` (like `i8` as well as `int`).
|
multiple actual types to be formatted via `{:x}` (like `i8` as well as `int`).
|
||||||
The current mapping of types to traits is:
|
The current mapping of types to traits is:
|
||||||
|
|
||||||
* *nothing* ⇒ `Show`
|
* *nothing* ⇒ `Show`
|
||||||
|
@ -157,12 +153,12 @@ When implementing a format trait for your own type, you will have to implement a
|
||||||
method of the signature:
|
method of the signature:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# use std;
|
# use std::fmt;
|
||||||
# mod fmt { pub type Result = (); }
|
# struct Foo; // our custom type
|
||||||
# struct T;
|
# impl fmt::Show for Foo {
|
||||||
# trait SomeName<T> {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result;
|
# write!(f, "testing, testing")
|
||||||
# }
|
# } }
|
||||||
```
|
```
|
||||||
|
|
||||||
Your type will be passed as `self` by-reference, and then the function should
|
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
|
||||||
format_args! // described below.
|
format_args! // described below.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
#### `write!`
|
#### `write!`
|
||||||
|
|
||||||
This and `writeln` are two macros which are used to emit the format string to a
|
This and `writeln` are two macros which are used to emit the format string to a
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue