Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
eb836dd6
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
eb836dd6
编写于
8月 23, 2013
作者:
A
Alex Crichton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Settle on the format/write/print family of names
上级
67512f71
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
295 addition
and
255 deletion
+295
-255
src/libstd/fmt/mod.rs
src/libstd/fmt/mod.rs
+26
-26
src/libsyntax/ext/base.rs
src/libsyntax/ext/base.rs
+6
-4
src/libsyntax/ext/expand.rs
src/libsyntax/ext/expand.rs
+17
-0
src/libsyntax/ext/ifmt.rs
src/libsyntax/ext/ifmt.rs
+13
-7
src/test/compile-fail/ifmt-bad-arg.rs
src/test/compile-fail/ifmt-bad-arg.rs
+43
-43
src/test/compile-fail/ifmt-bad-plural.rs
src/test/compile-fail/ifmt-bad-plural.rs
+1
-1
src/test/compile-fail/ifmt-bad-select.rs
src/test/compile-fail/ifmt-bad-select.rs
+1
-1
src/test/compile-fail/ifmt-unimpl.rs
src/test/compile-fail/ifmt-unimpl.rs
+1
-1
src/test/compile-fail/ifmt-unknown-trait.rs
src/test/compile-fail/ifmt-unknown-trait.rs
+1
-1
src/test/run-pass/ifmt.rs
src/test/run-pass/ifmt.rs
+186
-171
未找到文件。
src/libstd/fmt/mod.rs
浏览文件 @
eb836dd6
...
...
@@ -12,33 +12,33 @@
# The Formatting Module
This module contains the runtime support for the `
ifm
t!` syntax extension. This
This module contains the runtime support for the `
forma
t!` syntax extension. This
macro is implemented in the compiler to emit calls to this module in order to
format arguments at runtime into strings and streams.
The functions contained in this module should not normally be used in everyday
use cases of `
ifm
t!`. The assumptions made by these functions are unsafe for all
use cases of `
forma
t!`. The assumptions made by these functions are unsafe for all
inputs, and the compiler performs a large amount of validation on the arguments
to `
ifm
t!` in order to ensure safety at runtime. While it is possible to call
to `
forma
t!` in order to ensure safety at runtime. While it is possible to call
these functions directly, it is not recommended to do so in the general case.
## Usage
The `
ifm
t!` macro is intended to be familiar to those coming from C's
printf/
s
printf functions or Python's `str.format` function. In its current
revision, the `
ifm
t!` macro returns a `~str` type which is the result of the
The `
forma
t!` macro is intended to be familiar to those coming from C's
printf/
f
printf functions or Python's `str.format` function. In its current
revision, the `
forma
t!` macro returns a `~str` type which is the result of the
formatting. In the future it will also be able to pass in a stream to format
arguments directly while performing minimal allocations.
Some examples of the `
ifm
t!` extension are:
Some examples of the `
forma
t!` extension are:
~~~{.rust}
ifm
t!("Hello") // => ~"Hello"
ifm
t!("Hello, {:s}!", "world") // => ~"Hello, world!"
ifm
t!("The number is {:d}", 1) // => ~"The number is 1"
ifm
t!("{}", ~[3, 4]) // => ~"~[3, 4]"
ifm
t!("{value}", value=4) // => ~"4"
ifm
t!("{} {}", 1, 2) // => ~"1 2"
forma
t!("Hello") // => ~"Hello"
forma
t!("Hello, {:s}!", "world") // => ~"Hello, world!"
forma
t!("The number is {:d}", 1) // => ~"The number is 1"
forma
t!("{}", ~[3, 4]) // => ~"~[3, 4]"
forma
t!("{value}", value=4) // => ~"4"
forma
t!("{} {}", 1, 2) // => ~"1 2"
~~~
From these, you can see that the first argument is a format string. It is
...
...
@@ -62,7 +62,7 @@
### Named parameters
Rust itself does not have a Python-like equivalent of named parameters to a
function, but the `
ifm
t!` macro is a syntax extension which allows it to
function, but the `
forma
t!` macro is a syntax extension which allows it to
leverage named parameters. Named parameters are listed at the end of the
argument list and have the syntax:
...
...
@@ -146,7 +146,7 @@
## Internationalization
The formatting syntax supported by the `
ifm
t!` extension supports
The formatting syntax supported by the `
forma
t!` extension supports
internationalization by providing "methods" which execute various different
outputs depending on the input. The syntax and methods provided are similar to
other internationalization systems, so again nothing should seem alien.
...
...
@@ -164,7 +164,7 @@
example:
~~~
ifm
t!("{0, select, other{#}}", "hello") // => ~"hello"
forma
t!("{0, select, other{#}}", "hello") // => ~"hello"
~~~
This example is the equivalent of `{0:s}` essentially.
...
...
@@ -399,11 +399,11 @@ pub struct Argument<'self> {
#[allow(missing_doc)]
pub
trait
Float
{
fn
fmt
(
&
Self
,
&
mut
Formatter
);
}
/// The
fprintf
function takes an output stream, a precompiled format string,
/// The
`write`
function takes an output stream, a precompiled format string,
/// and a list of arguments. The arguments will be formatted according to the
/// specified format string into the output stream provided.
///
/// See the documentation for `
sprintf
` for why this function is unsafe and care
/// See the documentation for `
format
` for why this function is unsafe and care
/// should be taken if calling it manually.
///
/// Thankfully the rust compiler provides the macro `fmtf!` which will perform
...
...
@@ -419,8 +419,8 @@ pub struct Argument<'self> {
///
/// Note that this function assumes that there are enough arguments for the
/// format string.
pub
unsafe
fn
fprintf
(
output
:
&
mut
io
::
Writer
,
fmt
:
&
[
rt
::
Piece
],
args
:
&
[
Argument
])
{
pub
unsafe
fn
write
(
output
:
&
mut
io
::
Writer
,
fmt
:
&
[
rt
::
Piece
],
args
:
&
[
Argument
])
{
let
mut
formatter
=
Formatter
{
flags
:
0
,
width
:
None
,
...
...
@@ -436,7 +436,7 @@ pub unsafe fn fprintf(output: &mut io::Writer,
}
}
/// The
sprintf
function takes a precompiled format string and a list of
/// The
format
function takes a precompiled format string and a list of
/// arguments, to return the resulting formatted string.
///
/// This is currently an unsafe function because the types of all arguments
...
...
@@ -446,7 +446,7 @@ pub unsafe fn fprintf(output: &mut io::Writer,
/// for formatting the right type value. Because of this, the function is marked
/// as `unsafe` if this is being called manually.
///
/// Thankfully the rust compiler provides the macro `
ifm
t!` which will perform
/// Thankfully the rust compiler provides the macro `
forma
t!` which will perform
/// all of this validation at compile-time and provides a safe interface for
/// invoking this function.
///
...
...
@@ -458,9 +458,9 @@ pub unsafe fn fprintf(output: &mut io::Writer,
///
/// Note that this function assumes that there are enough arguments for the
/// format string.
pub
unsafe
fn
sprintf
(
fmt
:
&
[
rt
::
Piece
],
args
:
&
[
Argument
])
->
~
str
{
pub
unsafe
fn
format
(
fmt
:
&
[
rt
::
Piece
],
args
:
&
[
Argument
])
->
~
str
{
let
mut
output
=
MemWriter
::
new
();
fprintf
(
&
mut
output
as
&
mut
io
::
Writer
,
fmt
,
args
);
write
(
&
mut
output
as
&
mut
io
::
Writer
,
fmt
,
args
);
return
str
::
from_bytes_owned
(
output
.inner
());
}
...
...
@@ -468,7 +468,7 @@ impl<'self> Formatter<'self> {
// First up is the collection of functions used to execute a format string
// at runtime. This consumes all of the compile-time statics generated by
// the
ifm
t! syntax extension.
// the
forma
t! syntax extension.
fn
run
(
&
mut
self
,
piece
:
&
rt
::
Piece
,
cur
:
Option
<&
str
>
)
{
let
setcount
=
|
slot
:
&
mut
Option
<
uint
>
,
cnt
:
&
parse
::
Count
|
{
...
...
@@ -732,7 +732,7 @@ fn with_padding(&mut self, padding: uint,
}
/// This is a function which calls are emitted to by the compiler itself to
/// create the Argument structures that are passed into the `
sprintf
` function.
/// create the Argument structures that are passed into the `
format
` function.
#[doc(hidden)]
pub
fn
argument
<
'a
,
T
>
(
f
:
extern
"Rust"
fn
(
&
T
,
&
mut
Formatter
),
t
:
&
'a
T
)
->
Argument
<
'a
>
{
...
...
src/libsyntax/ext/base.rs
浏览文件 @
eb836dd6
...
...
@@ -139,10 +139,12 @@ fn builtin_item_tt(f: SyntaxExpanderTTItemFun) -> @Transformer {
ext
::
tt
::
macro_rules
::
add_new_extension
));
syntax_expanders
.insert
(
intern
(
&
"fmt"
),
builtin_normal_tt
(
ext
::
fmt
::
expand_syntax_ext
));
syntax_expanders
.insert
(
intern
(
&
"ifmt"
),
builtin_normal_tt
(
ext
::
ifmt
::
expand_sprintf
));
syntax_expanders
.insert
(
intern
(
&
"ifmtf"
),
builtin_normal_tt
(
ext
::
ifmt
::
expand_fprintf
));
syntax_expanders
.insert
(
intern
(
&
"format"
),
builtin_normal_tt
(
ext
::
ifmt
::
expand_format
));
syntax_expanders
.insert
(
intern
(
&
"write"
),
builtin_normal_tt
(
ext
::
ifmt
::
expand_write
));
syntax_expanders
.insert
(
intern
(
&
"writeln"
),
builtin_normal_tt
(
ext
::
ifmt
::
expand_writeln
));
syntax_expanders
.insert
(
intern
(
&
"auto_encode"
),
@
SE
(
ItemDecorator
(
ext
::
auto_encode
::
expand_auto_encode
)));
...
...
src/libsyntax/ext/expand.rs
浏览文件 @
eb836dd6
...
...
@@ -940,6 +940,7 @@ pub mod $c {
);
)
// NOTE(acrichto): start removing this after the next snapshot
macro_rules! printf (
($arg:expr) => (
print(fmt!(
\"
%?
\"
, $arg))
...
...
@@ -949,6 +950,7 @@ pub mod $c {
)
)
// NOTE(acrichto): start removing this after the next snapshot
macro_rules! printfln (
($arg:expr) => (
println(fmt!(
\"
%?
\"
, $arg))
...
...
@@ -958,6 +960,21 @@ pub mod $c {
)
)
// FIXME(#6846) once stdio is redesigned, this shouldn't perform an
// allocation but should rather delegate to an invocation of
// write! instead of format!
macro_rules! print (
() => ();
($arg:expr) => ( ::std::io::print(format!(
\"
{}
\"
, $arg)));
($fmt:expr, $($arg:tt)+) => ( ::std::io::print(format!($fmt, $($arg)+)))
)
// FIXME(#6846) once stdio is redesigned, this shouldn't perform an
// allocation but should rather delegate to an io::Writer
macro_rules! println (
($($arg:tt)*) => ({ print!($($arg)*); ::std::io::println(
\"\"
); })
)
// NOTE: use this after a snapshot lands to abstract the details
// of the TLS interface.
macro_rules! local_data_key (
...
...
src/libsyntax/ext/ifmt.rs
浏览文件 @
eb836dd6
...
...
@@ -697,19 +697,24 @@ fn format_arg(&self, sp: span, arg: Either<uint, @str>,
}
}
pub
fn
expand_
sprintf
(
ecx
:
@
ExtCtxt
,
sp
:
span
,
tts
:
&
[
ast
::
token_tree
])
->
base
::
MacResult
{
expand_ifmt
(
ecx
,
sp
,
tts
,
false
,
"sprintf
"
)
pub
fn
expand_
format
(
ecx
:
@
ExtCtxt
,
sp
:
span
,
tts
:
&
[
ast
::
token_tree
])
->
base
::
MacResult
{
expand_ifmt
(
ecx
,
sp
,
tts
,
false
,
false
,
"format
"
)
}
pub
fn
expand_
fprintf
(
ecx
:
@
ExtCtxt
,
sp
:
span
,
tts
:
&
[
ast
::
token_tree
])
->
base
::
MacResult
{
expand_ifmt
(
ecx
,
sp
,
tts
,
true
,
"fprintf
"
)
pub
fn
expand_
write
(
ecx
:
@
ExtCtxt
,
sp
:
span
,
tts
:
&
[
ast
::
token_tree
])
->
base
::
MacResult
{
expand_ifmt
(
ecx
,
sp
,
tts
,
true
,
false
,
"write
"
)
}
pub
fn
expand_writeln
(
ecx
:
@
ExtCtxt
,
sp
:
span
,
tts
:
&
[
ast
::
token_tree
])
->
base
::
MacResult
{
expand_ifmt
(
ecx
,
sp
,
tts
,
true
,
true
,
"write"
)
}
fn
expand_ifmt
(
ecx
:
@
ExtCtxt
,
sp
:
span
,
tts
:
&
[
ast
::
token_tree
],
leading_arg
:
bool
,
function
:
&
str
)
->
base
::
MacResult
{
leading_arg
:
bool
,
append_newline
:
bool
,
function
:
&
str
)
->
base
::
MacResult
{
let
mut
cx
=
Context
{
ecx
:
ecx
,
args
:
~
[],
...
...
@@ -730,6 +735,7 @@ fn expand_ifmt(ecx: @ExtCtxt, sp: span, tts: &[ast::token_tree],
cx
.fmtsp
=
efmt
.span
;
let
fmt
=
expr_to_str
(
ecx
,
efmt
,
"format argument must be a string literal."
);
let
fmt
=
if
append_newline
{
fmt
+
"
\n
"
}
else
{
fmt
.to_owned
()
};
let
mut
err
=
false
;
do
parse
::
parse_error
::
cond
.trap
(|
m
|
{
...
...
src/test/compile-fail/ifmt-bad-arg.rs
浏览文件 @
eb836dd6
...
...
@@ -9,66 +9,66 @@
// except according to those terms.
fn
main
()
{
// bad arguments to the
ifm
t! call
// bad arguments to the
forma
t! call
ifmt!
();
//~ ERROR: expects at least one
ifm
t!
(
"{}"
);
//~ ERROR: invalid reference to argument
format!
();
//~ ERROR: requires at least a format string
forma
t!
(
"{}"
);
//~ ERROR: invalid reference to argument
ifm
t!
(
"{1}"
,
1
);
//~ ERROR: invalid reference to argument `1`
forma
t!
(
"{1}"
,
1
);
//~ ERROR: invalid reference to argument `1`
//~^ ERROR: argument never used
ifm
t!
(
"{foo}"
);
//~ ERROR: no argument named `foo`
forma
t!
(
"{foo}"
);
//~ ERROR: no argument named `foo`
ifm
t!
(
"{}"
,
1
,
2
);
//~ ERROR: argument never used
ifm
t!
(
"{1}"
,
1
,
2
);
//~ ERROR: argument never used
ifm
t!
(
"{}"
,
1
,
foo
=
2
);
//~ ERROR: named argument never used
ifm
t!
(
"{foo}"
,
1
,
foo
=
2
);
//~ ERROR: argument never used
ifm
t!
(
""
,
foo
=
2
);
//~ ERROR: named argument never used
forma
t!
(
"{}"
,
1
,
2
);
//~ ERROR: argument never used
forma
t!
(
"{1}"
,
1
,
2
);
//~ ERROR: argument never used
forma
t!
(
"{}"
,
1
,
foo
=
2
);
//~ ERROR: named argument never used
forma
t!
(
"{foo}"
,
1
,
foo
=
2
);
//~ ERROR: argument never used
forma
t!
(
""
,
foo
=
2
);
//~ ERROR: named argument never used
ifm
t!
(
"{0:d} {0:s}"
,
1
);
//~ ERROR: redeclared with type `s`
ifm
t!
(
"{foo:d} {foo:s}"
,
foo
=
1
);
//~ ERROR: redeclared with type `s`
forma
t!
(
"{0:d} {0:s}"
,
1
);
//~ ERROR: redeclared with type `s`
forma
t!
(
"{foo:d} {foo:s}"
,
foo
=
1
);
//~ ERROR: redeclared with type `s`
ifm
t!
(
"{foo}"
,
foo
=
1
,
foo
=
2
);
//~ ERROR: duplicate argument
ifm
t!
(
"#"
);
//~ ERROR: `#` reference used
ifm
t!
(
""
,
foo
=
1
,
2
);
//~ ERROR: positional arguments cannot follow
ifm
t!
(
""
1
);
//~ ERROR: expected token: `,`
ifm
t!
(
""
,
1
1
);
//~ ERROR: expected token: `,`
forma
t!
(
"{foo}"
,
foo
=
1
,
foo
=
2
);
//~ ERROR: duplicate argument
forma
t!
(
"#"
);
//~ ERROR: `#` reference used
forma
t!
(
""
,
foo
=
1
,
2
);
//~ ERROR: positional arguments cannot follow
forma
t!
(
""
1
);
//~ ERROR: expected token: `,`
forma
t!
(
""
,
1
1
);
//~ ERROR: expected token: `,`
ifm
t!
(
"{0, select, a{} a{} other{}}"
,
"a"
);
//~ ERROR: duplicate selector
ifm
t!
(
"{0, plural, =1{} =1{} other{}}"
,
1u
);
//~ ERROR: duplicate selector
ifm
t!
(
"{0, plural, one{} one{} other{}}"
,
1u
);
//~ ERROR: duplicate selector
forma
t!
(
"{0, select, a{} a{} other{}}"
,
"a"
);
//~ ERROR: duplicate selector
forma
t!
(
"{0, plural, =1{} =1{} other{}}"
,
1u
);
//~ ERROR: duplicate selector
forma
t!
(
"{0, plural, one{} one{} other{}}"
,
1u
);
//~ ERROR: duplicate selector
// bad syntax of the format string
ifm
t!
(
"{"
);
//~ ERROR: unterminated format string
ifm
t!
(
"
\\
"
);
//~ ERROR: invalid escape
ifm
t!
(
"
\\
"
);
//~ ERROR: expected an escape
forma
t!
(
"{"
);
//~ ERROR: unterminated format string
forma
t!
(
"
\\
"
);
//~ ERROR: invalid escape
forma
t!
(
"
\\
"
);
//~ ERROR: expected an escape
ifm
t!
(
"{0, }"
,
1
);
//~ ERROR: expected method
ifm
t!
(
"{0, foo}"
,
1
);
//~ ERROR: unknown method
ifm
t!
(
"{0, select}"
,
"a"
);
//~ ERROR: must be followed by
ifm
t!
(
"{0, plural}"
,
1
);
//~ ERROR: must be followed by
forma
t!
(
"{0, }"
,
1
);
//~ ERROR: expected method
forma
t!
(
"{0, foo}"
,
1
);
//~ ERROR: unknown method
forma
t!
(
"{0, select}"
,
"a"
);
//~ ERROR: must be followed by
forma
t!
(
"{0, plural}"
,
1
);
//~ ERROR: must be followed by
ifm
t!
(
"{0, select, a{{}"
,
1
);
//~ ERROR: must be terminated
ifm
t!
(
"{0, select, {} other{}}"
,
"a"
);
//~ ERROR: empty selector
ifm
t!
(
"{0, select, other{} other{}}"
,
"a"
);
//~ ERROR: multiple `other`
ifm
t!
(
"{0, plural, offset: other{}}"
,
"a"
);
//~ ERROR: must be an integer
ifm
t!
(
"{0, plural, offset 1 other{}}"
,
"a"
);
//~ ERROR: be followed by `:`
ifm
t!
(
"{0, plural, =a{} other{}}"
,
"a"
);
//~ ERROR: followed by an integer
ifm
t!
(
"{0, plural, a{} other{}}"
,
"a"
);
//~ ERROR: unexpected plural
ifm
t!
(
"{0, select, a{}}"
,
"a"
);
//~ ERROR: must provide an `other`
ifm
t!
(
"{0, plural, =1{}}"
,
"a"
);
//~ ERROR: must provide an `other`
forma
t!
(
"{0, select, a{{}"
,
1
);
//~ ERROR: must be terminated
forma
t!
(
"{0, select, {} other{}}"
,
"a"
);
//~ ERROR: empty selector
forma
t!
(
"{0, select, other{} other{}}"
,
"a"
);
//~ ERROR: multiple `other`
forma
t!
(
"{0, plural, offset: other{}}"
,
"a"
);
//~ ERROR: must be an integer
forma
t!
(
"{0, plural, offset 1 other{}}"
,
"a"
);
//~ ERROR: be followed by `:`
forma
t!
(
"{0, plural, =a{} other{}}"
,
"a"
);
//~ ERROR: followed by an integer
forma
t!
(
"{0, plural, a{} other{}}"
,
"a"
);
//~ ERROR: unexpected plural
forma
t!
(
"{0, select, a{}}"
,
"a"
);
//~ ERROR: must provide an `other`
forma
t!
(
"{0, plural, =1{}}"
,
"a"
);
//~ ERROR: must provide an `other`
ifm
t!
(
"{0, plural, other{{0:s}}}"
,
"a"
);
//~ ERROR: previously used as
ifm
t!
(
"{:s} {0, plural, other{}}"
,
"a"
);
//~ ERROR: argument used to
ifm
t!
(
"{0, select, other{}}
\
{0, plural, other{}}"
,
"a"
);
forma
t!
(
"{0, plural, other{{0:s}}}"
,
"a"
);
//~ ERROR: previously used as
forma
t!
(
"{:s} {0, plural, other{}}"
,
"a"
);
//~ ERROR: argument used to
forma
t!
(
"{0, select, other{}}
\
{0, plural, other{}}"
,
"a"
);
//~^ ERROR: declared with multiple formats
// It should be illegal to use implicit placement arguments nested inside of
// format strings because otherwise the "internal pointer of which argument
// is next" would be invalidated if different cases had different numbers of
// arguments.
ifm
t!
(
"{0, select, other{{}}}"
,
"a"
);
//~ ERROR: cannot use implicit
ifm
t!
(
"{0, plural, other{{}}}"
,
1
);
//~ ERROR: cannot use implicit
ifm
t!
(
"{0, plural, other{{1:.*d}}}"
,
1
,
2
);
//~ ERROR: cannot use implicit
forma
t!
(
"{0, select, other{{}}}"
,
"a"
);
//~ ERROR: cannot use implicit
forma
t!
(
"{0, plural, other{{}}}"
,
1
);
//~ ERROR: cannot use implicit
forma
t!
(
"{0, plural, other{{1:.*d}}}"
,
1
,
2
);
//~ ERROR: cannot use implicit
}
src/test/compile-fail/ifmt-bad-plural.rs
浏览文件 @
eb836dd6
...
...
@@ -9,6 +9,6 @@
// except according to those terms.
fn
main
()
{
ifm
t!
(
"{0, plural, other{}}"
,
"a"
);
forma
t!
(
"{0, plural, other{}}"
,
"a"
);
//~^ ERROR: expected uint but found
}
src/test/compile-fail/ifmt-bad-select.rs
浏览文件 @
eb836dd6
...
...
@@ -9,6 +9,6 @@
// except according to those terms.
fn
main
()
{
ifm
t!
(
"{0, select, other{}}"
,
2
);
forma
t!
(
"{0, select, other{}}"
,
2
);
//~^ ERROR: expected &str but found integral
}
src/test/compile-fail/ifmt-unimpl.rs
浏览文件 @
eb836dd6
...
...
@@ -9,6 +9,6 @@
// except according to those terms.
fn
main
()
{
ifm
t!
(
"{:d}"
,
"3"
);
forma
t!
(
"{:d}"
,
"3"
);
//~^ ERROR: failed to find an implementation of trait std::fmt::Signed
}
src/test/compile-fail/ifmt-unknown-trait.rs
浏览文件 @
eb836dd6
...
...
@@ -9,6 +9,6 @@
// except according to those terms.
fn
main
()
{
ifm
t!
(
"{:notimplemented}"
,
"3"
);
forma
t!
(
"{:notimplemented}"
,
"3"
);
//~^ ERROR: unknown format trait `notimplemented`
}
src/test/run-pass/ifmt.rs
浏览文件 @
eb836dd6
...
...
@@ -26,208 +26,223 @@ impl fmt::Signed for B {
pub
fn
main
()
{
// Make sure there's a poly formatter that takes anything
t!
(
ifm
t!
(
"{:?}"
,
1
),
"1"
);
t!
(
ifm
t!
(
"{:?}"
,
A
),
"{}"
);
t!
(
ifm
t!
(
"{:?}"
,
()),
"()"
);
t!
(
ifm
t!
(
"{:?}"
,
@
(
~
1
,
"foo"
)),
"@(~1,
\"
foo
\"
)"
);
t!
(
forma
t!
(
"{:?}"
,
1
),
"1"
);
t!
(
forma
t!
(
"{:?}"
,
A
),
"{}"
);
t!
(
forma
t!
(
"{:?}"
,
()),
"()"
);
t!
(
forma
t!
(
"{:?}"
,
@
(
~
1
,
"foo"
)),
"@(~1,
\"
foo
\"
)"
);
// Various edge cases without formats
t!
(
ifm
t!
(
""
),
""
);
t!
(
ifm
t!
(
"hello"
),
"hello"
);
t!
(
ifm
t!
(
"hello
\\
{"
),
"hello {"
);
t!
(
forma
t!
(
""
),
""
);
t!
(
forma
t!
(
"hello"
),
"hello"
);
t!
(
forma
t!
(
"hello
\\
{"
),
"hello {"
);
// default formatters should work
t!
(
ifm
t!
(
"{}"
,
1
i
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1i8
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1i16
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1i32
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1i64
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1u
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1u8
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1u16
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1u32
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1u64
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1.0
f
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1.0f32
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
1.0f64
),
"1"
);
t!
(
ifm
t!
(
"{}"
,
"a"
),
"a"
);
t!
(
ifm
t!
(
"{}"
,
~
"a"
),
"a"
);
t!
(
ifm
t!
(
"{}"
,
@
"a"
),
"a"
);
t!
(
ifm
t!
(
"{}"
,
false
),
"false"
);
t!
(
ifm
t!
(
"{}"
,
'a'
),
"a"
);
t!
(
forma
t!
(
"{}"
,
1
i
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1i8
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1i16
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1i32
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1i64
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1u
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1u8
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1u16
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1u32
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1u64
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1.0
f
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1.0f32
),
"1"
);
t!
(
forma
t!
(
"{}"
,
1.0f64
),
"1"
);
t!
(
forma
t!
(
"{}"
,
"a"
),
"a"
);
t!
(
forma
t!
(
"{}"
,
~
"a"
),
"a"
);
t!
(
forma
t!
(
"{}"
,
@
"a"
),
"a"
);
t!
(
forma
t!
(
"{}"
,
false
),
"false"
);
t!
(
forma
t!
(
"{}"
,
'a'
),
"a"
);
// At least exercise all the formats
t!
(
ifm
t!
(
"{:b}"
,
true
),
"true"
);
t!
(
ifm
t!
(
"{:c}"
,
'☃'
),
"☃"
);
t!
(
ifm
t!
(
"{:d}"
,
10
),
"10"
);
t!
(
ifm
t!
(
"{:i}"
,
10
),
"10"
);
t!
(
ifm
t!
(
"{:u}"
,
10u
),
"10"
);
t!
(
ifm
t!
(
"{:o}"
,
10u
),
"12"
);
t!
(
ifm
t!
(
"{:x}"
,
10u
),
"a"
);
t!
(
ifm
t!
(
"{:X}"
,
10u
),
"A"
);
t!
(
ifm
t!
(
"{:s}"
,
"foo"
),
"foo"
);
t!
(
ifm
t!
(
"{:s}"
,
~
"foo"
),
"foo"
);
t!
(
ifm
t!
(
"{:s}"
,
@
"foo"
),
"foo"
);
t!
(
ifm
t!
(
"{:p}"
,
0x1234
as
*
int
),
"0x1234"
);
t!
(
ifm
t!
(
"{:p}"
,
0x1234
as
*
mut
int
),
"0x1234"
);
t!
(
ifm
t!
(
"{:d}"
,
A
),
"aloha"
);
t!
(
ifm
t!
(
"{:d}"
,
B
),
"adios"
);
t!
(
ifm
t!
(
"foo {:s} ☃☃☃☃☃☃"
,
"bar"
),
"foo bar ☃☃☃☃☃☃"
);
t!
(
ifm
t!
(
"{1} {0}"
,
0
,
1
),
"1 0"
);
t!
(
ifm
t!
(
"{foo} {bar}"
,
foo
=
0
,
bar
=
1
),
"0 1"
);
t!
(
ifm
t!
(
"{foo} {1} {bar} {0}"
,
0
,
1
,
foo
=
2
,
bar
=
3
),
"2 1 3 0"
);
t!
(
ifm
t!
(
"{} {0:s}"
,
"a"
),
"a a"
);
t!
(
ifm
t!
(
"{} {0}"
,
"a"
),
"a a"
);
t!
(
forma
t!
(
"{:b}"
,
true
),
"true"
);
t!
(
forma
t!
(
"{:c}"
,
'☃'
),
"☃"
);
t!
(
forma
t!
(
"{:d}"
,
10
),
"10"
);
t!
(
forma
t!
(
"{:i}"
,
10
),
"10"
);
t!
(
forma
t!
(
"{:u}"
,
10u
),
"10"
);
t!
(
forma
t!
(
"{:o}"
,
10u
),
"12"
);
t!
(
forma
t!
(
"{:x}"
,
10u
),
"a"
);
t!
(
forma
t!
(
"{:X}"
,
10u
),
"A"
);
t!
(
forma
t!
(
"{:s}"
,
"foo"
),
"foo"
);
t!
(
forma
t!
(
"{:s}"
,
~
"foo"
),
"foo"
);
t!
(
forma
t!
(
"{:s}"
,
@
"foo"
),
"foo"
);
t!
(
forma
t!
(
"{:p}"
,
0x1234
as
*
int
),
"0x1234"
);
t!
(
forma
t!
(
"{:p}"
,
0x1234
as
*
mut
int
),
"0x1234"
);
t!
(
forma
t!
(
"{:d}"
,
A
),
"aloha"
);
t!
(
forma
t!
(
"{:d}"
,
B
),
"adios"
);
t!
(
forma
t!
(
"foo {:s} ☃☃☃☃☃☃"
,
"bar"
),
"foo bar ☃☃☃☃☃☃"
);
t!
(
forma
t!
(
"{1} {0}"
,
0
,
1
),
"1 0"
);
t!
(
forma
t!
(
"{foo} {bar}"
,
foo
=
0
,
bar
=
1
),
"0 1"
);
t!
(
forma
t!
(
"{foo} {1} {bar} {0}"
,
0
,
1
,
foo
=
2
,
bar
=
3
),
"2 1 3 0"
);
t!
(
forma
t!
(
"{} {0:s}"
,
"a"
),
"a a"
);
t!
(
forma
t!
(
"{} {0}"
,
"a"
),
"a a"
);
// Methods should probably work
t!
(
ifm
t!
(
"{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}"
,
0u
),
"c0"
);
t!
(
ifm
t!
(
"{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}"
,
1u
),
"a1"
);
t!
(
ifm
t!
(
"{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}"
,
2u
),
"b2"
);
t!
(
ifm
t!
(
"{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}"
,
3u
),
"d3"
);
t!
(
ifm
t!
(
"{0, select, a{a#} b{b#} c{c#} other{d#}}"
,
"a"
),
"aa"
);
t!
(
ifm
t!
(
"{0, select, a{a#} b{b#} c{c#} other{d#}}"
,
"b"
),
"bb"
);
t!
(
ifm
t!
(
"{0, select, a{a#} b{b#} c{c#} other{d#}}"
,
"c"
),
"cc"
);
t!
(
ifm
t!
(
"{0, select, a{a#} b{b#} c{c#} other{d#}}"
,
"d"
),
"dd"
);
t!
(
ifm
t!
(
"{1, select, a{#{0:s}} other{#{1}}}"
,
"b"
,
"a"
),
"ab"
);
t!
(
ifm
t!
(
"{1, select, a{#{0}} other{#{1}}}"
,
"c"
,
"b"
),
"bb"
);
t!
(
forma
t!
(
"{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}"
,
0u
),
"c0"
);
t!
(
forma
t!
(
"{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}"
,
1u
),
"a1"
);
t!
(
forma
t!
(
"{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}"
,
2u
),
"b2"
);
t!
(
forma
t!
(
"{0, plural, =1{a#} =2{b#} zero{c#} other{d#}}"
,
3u
),
"d3"
);
t!
(
forma
t!
(
"{0, select, a{a#} b{b#} c{c#} other{d#}}"
,
"a"
),
"aa"
);
t!
(
forma
t!
(
"{0, select, a{a#} b{b#} c{c#} other{d#}}"
,
"b"
),
"bb"
);
t!
(
forma
t!
(
"{0, select, a{a#} b{b#} c{c#} other{d#}}"
,
"c"
),
"cc"
);
t!
(
forma
t!
(
"{0, select, a{a#} b{b#} c{c#} other{d#}}"
,
"d"
),
"dd"
);
t!
(
forma
t!
(
"{1, select, a{#{0:s}} other{#{1}}}"
,
"b"
,
"a"
),
"ab"
);
t!
(
forma
t!
(
"{1, select, a{#{0}} other{#{1}}}"
,
"c"
,
"b"
),
"bb"
);
// Formatting strings and their arguments
t!
(
ifm
t!
(
"{:s}"
,
"a"
),
"a"
);
t!
(
ifm
t!
(
"{:4s}"
,
"a"
),
"a "
);
t!
(
ifm
t!
(
"{:>4s}"
,
"a"
),
" a"
);
t!
(
ifm
t!
(
"{:<4s}"
,
"a"
),
"a "
);
t!
(
ifm
t!
(
"{:.4s}"
,
"a"
),
"a"
);
t!
(
ifm
t!
(
"{:4.4s}"
,
"a"
),
"a "
);
t!
(
ifm
t!
(
"{:4.4s}"
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
ifm
t!
(
"{:<4.4s}"
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
ifm
t!
(
"{:>4.4s}"
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
ifm
t!
(
"{:>10.4s}"
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
ifm
t!
(
"{:2.4s}"
,
"aaaaa"
),
"aaaa"
);
t!
(
ifm
t!
(
"{:2.4s}"
,
"aaaa"
),
"aaaa"
);
t!
(
ifm
t!
(
"{:2.4s}"
,
"aaa"
),
"aaa"
);
t!
(
ifm
t!
(
"{:2.4s}"
,
"aa"
),
"aa"
);
t!
(
ifm
t!
(
"{:2.4s}"
,
"a"
),
"a "
);
t!
(
ifm
t!
(
"{:0>2s}"
,
"a"
),
"0a"
);
t!
(
ifm
t!
(
"{:.*s}"
,
4
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
ifm
t!
(
"{:.1$s}"
,
"aaaaaaaaaaaaaaaaaa"
,
4
),
"aaaa"
);
t!
(
ifm
t!
(
"{:1$s}"
,
"a"
,
4
),
"a "
);
t!
(
ifm
t!
(
"{:-#s}"
,
"a"
),
"a"
);
t!
(
ifm
t!
(
"{:+#s}"
,
"a"
),
"a"
);
t!
(
forma
t!
(
"{:s}"
,
"a"
),
"a"
);
t!
(
forma
t!
(
"{:4s}"
,
"a"
),
"a "
);
t!
(
forma
t!
(
"{:>4s}"
,
"a"
),
" a"
);
t!
(
forma
t!
(
"{:<4s}"
,
"a"
),
"a "
);
t!
(
forma
t!
(
"{:.4s}"
,
"a"
),
"a"
);
t!
(
forma
t!
(
"{:4.4s}"
,
"a"
),
"a "
);
t!
(
forma
t!
(
"{:4.4s}"
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
forma
t!
(
"{:<4.4s}"
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
forma
t!
(
"{:>4.4s}"
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
forma
t!
(
"{:>10.4s}"
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
forma
t!
(
"{:2.4s}"
,
"aaaaa"
),
"aaaa"
);
t!
(
forma
t!
(
"{:2.4s}"
,
"aaaa"
),
"aaaa"
);
t!
(
forma
t!
(
"{:2.4s}"
,
"aaa"
),
"aaa"
);
t!
(
forma
t!
(
"{:2.4s}"
,
"aa"
),
"aa"
);
t!
(
forma
t!
(
"{:2.4s}"
,
"a"
),
"a "
);
t!
(
forma
t!
(
"{:0>2s}"
,
"a"
),
"0a"
);
t!
(
forma
t!
(
"{:.*s}"
,
4
,
"aaaaaaaaaaaaaaaaaa"
),
"aaaa"
);
t!
(
forma
t!
(
"{:.1$s}"
,
"aaaaaaaaaaaaaaaaaa"
,
4
),
"aaaa"
);
t!
(
forma
t!
(
"{:1$s}"
,
"a"
,
4
),
"a "
);
t!
(
forma
t!
(
"{:-#s}"
,
"a"
),
"a"
);
t!
(
forma
t!
(
"{:+#s}"
,
"a"
),
"a"
);
// Formatting integers should select the right implementation based off the
// type of the argument. Also, hex/octal/binary should be defined for
// integers, but they shouldn't emit the negative sign.
t!
(
ifm
t!
(
"{:d}"
,
-
1
i
),
"-1"
);
t!
(
ifm
t!
(
"{:d}"
,
-
1i8
),
"-1"
);
t!
(
ifm
t!
(
"{:d}"
,
-
1i16
),
"-1"
);
t!
(
ifm
t!
(
"{:d}"
,
-
1i32
),
"-1"
);
t!
(
ifm
t!
(
"{:d}"
,
-
1i64
),
"-1"
);
t!
(
ifm
t!
(
"{:t}"
,
1
i
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1i8
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1i16
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1i32
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1i64
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1
i
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1i8
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1i16
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1i32
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1i64
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1
i
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1i8
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1i16
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1i32
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1i64
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1
i
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1i8
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1i16
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1i32
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1i64
),
"1"
);
t!
(
ifm
t!
(
"{:u}"
,
1u
),
"1"
);
t!
(
ifm
t!
(
"{:u}"
,
1u8
),
"1"
);
t!
(
ifm
t!
(
"{:u}"
,
1u16
),
"1"
);
t!
(
ifm
t!
(
"{:u}"
,
1u32
),
"1"
);
t!
(
ifm
t!
(
"{:u}"
,
1u64
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1u
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1u8
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1u16
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1u32
),
"1"
);
t!
(
ifm
t!
(
"{:t}"
,
1u64
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1u
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1u8
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1u16
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1u32
),
"1"
);
t!
(
ifm
t!
(
"{:x}"
,
1u64
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1u
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1u8
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1u16
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1u32
),
"1"
);
t!
(
ifm
t!
(
"{:X}"
,
1u64
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1u
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1u8
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1u16
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1u32
),
"1"
);
t!
(
ifm
t!
(
"{:o}"
,
1u64
),
"1"
);
t!
(
forma
t!
(
"{:d}"
,
-
1
i
),
"-1"
);
t!
(
forma
t!
(
"{:d}"
,
-
1i8
),
"-1"
);
t!
(
forma
t!
(
"{:d}"
,
-
1i16
),
"-1"
);
t!
(
forma
t!
(
"{:d}"
,
-
1i32
),
"-1"
);
t!
(
forma
t!
(
"{:d}"
,
-
1i64
),
"-1"
);
t!
(
forma
t!
(
"{:t}"
,
1
i
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1i8
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1i16
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1i32
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1i64
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1
i
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1i8
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1i16
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1i32
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1i64
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1
i
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1i8
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1i16
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1i32
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1i64
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1
i
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1i8
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1i16
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1i32
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1i64
),
"1"
);
t!
(
forma
t!
(
"{:u}"
,
1u
),
"1"
);
t!
(
forma
t!
(
"{:u}"
,
1u8
),
"1"
);
t!
(
forma
t!
(
"{:u}"
,
1u16
),
"1"
);
t!
(
forma
t!
(
"{:u}"
,
1u32
),
"1"
);
t!
(
forma
t!
(
"{:u}"
,
1u64
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1u
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1u8
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1u16
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1u32
),
"1"
);
t!
(
forma
t!
(
"{:t}"
,
1u64
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1u
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1u8
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1u16
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1u32
),
"1"
);
t!
(
forma
t!
(
"{:x}"
,
1u64
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1u
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1u8
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1u16
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1u32
),
"1"
);
t!
(
forma
t!
(
"{:X}"
,
1u64
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1u
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1u8
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1u16
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1u32
),
"1"
);
t!
(
forma
t!
(
"{:o}"
,
1u64
),
"1"
);
// Test the flags for formatting integers
t!
(
ifm
t!
(
"{:3d}"
,
1
),
" 1"
);
t!
(
ifm
t!
(
"{:>3d}"
,
1
),
" 1"
);
t!
(
ifm
t!
(
"{:>+3d}"
,
1
),
" +1"
);
t!
(
ifm
t!
(
"{:<3d}"
,
1
),
"1 "
);
t!
(
ifm
t!
(
"{:#d}"
,
1
),
"1"
);
t!
(
ifm
t!
(
"{:#x}"
,
10
),
"0xa"
);
t!
(
ifm
t!
(
"{:#X}"
,
10
),
"0xA"
);
t!
(
ifm
t!
(
"{:#5x}"
,
10
),
" 0xa"
);
t!
(
ifm
t!
(
"{:#o}"
,
10
),
"0o12"
);
t!
(
ifm
t!
(
"{:08x}"
,
10
),
"0000000a"
);
t!
(
ifm
t!
(
"{:8x}"
,
10
),
" a"
);
t!
(
ifm
t!
(
"{:<8x}"
,
10
),
"a "
);
t!
(
ifm
t!
(
"{:>8x}"
,
10
),
" a"
);
t!
(
ifm
t!
(
"{:#08x}"
,
10
),
"0x00000a"
);
t!
(
ifm
t!
(
"{:08d}"
,
-
10
),
"-0000010"
);
t!
(
ifm
t!
(
"{:x}"
,
-
1u8
),
"ff"
);
t!
(
ifm
t!
(
"{:X}"
,
-
1u8
),
"FF"
);
t!
(
ifm
t!
(
"{:t}"
,
-
1u8
),
"11111111"
);
t!
(
ifm
t!
(
"{:o}"
,
-
1u8
),
"377"
);
t!
(
ifm
t!
(
"{:#x}"
,
-
1u8
),
"0xff"
);
t!
(
ifm
t!
(
"{:#X}"
,
-
1u8
),
"0xFF"
);
t!
(
ifm
t!
(
"{:#t}"
,
-
1u8
),
"0b11111111"
);
t!
(
ifm
t!
(
"{:#o}"
,
-
1u8
),
"0o377"
);
t!
(
forma
t!
(
"{:3d}"
,
1
),
" 1"
);
t!
(
forma
t!
(
"{:>3d}"
,
1
),
" 1"
);
t!
(
forma
t!
(
"{:>+3d}"
,
1
),
" +1"
);
t!
(
forma
t!
(
"{:<3d}"
,
1
),
"1 "
);
t!
(
forma
t!
(
"{:#d}"
,
1
),
"1"
);
t!
(
forma
t!
(
"{:#x}"
,
10
),
"0xa"
);
t!
(
forma
t!
(
"{:#X}"
,
10
),
"0xA"
);
t!
(
forma
t!
(
"{:#5x}"
,
10
),
" 0xa"
);
t!
(
forma
t!
(
"{:#o}"
,
10
),
"0o12"
);
t!
(
forma
t!
(
"{:08x}"
,
10
),
"0000000a"
);
t!
(
forma
t!
(
"{:8x}"
,
10
),
" a"
);
t!
(
forma
t!
(
"{:<8x}"
,
10
),
"a "
);
t!
(
forma
t!
(
"{:>8x}"
,
10
),
" a"
);
t!
(
forma
t!
(
"{:#08x}"
,
10
),
"0x00000a"
);
t!
(
forma
t!
(
"{:08d}"
,
-
10
),
"-0000010"
);
t!
(
forma
t!
(
"{:x}"
,
-
1u8
),
"ff"
);
t!
(
forma
t!
(
"{:X}"
,
-
1u8
),
"FF"
);
t!
(
forma
t!
(
"{:t}"
,
-
1u8
),
"11111111"
);
t!
(
forma
t!
(
"{:o}"
,
-
1u8
),
"377"
);
t!
(
forma
t!
(
"{:#x}"
,
-
1u8
),
"0xff"
);
t!
(
forma
t!
(
"{:#X}"
,
-
1u8
),
"0xFF"
);
t!
(
forma
t!
(
"{:#t}"
,
-
1u8
),
"0b11111111"
);
t!
(
forma
t!
(
"{:#o}"
,
-
1u8
),
"0o377"
);
// Signed combinations
t!
(
ifm
t!
(
"{:+5d}"
,
1
),
" +1"
);
t!
(
ifm
t!
(
"{:+5d}"
,
-
1
),
" -1"
);
t!
(
ifm
t!
(
"{:05d}"
,
1
),
"00001"
);
t!
(
ifm
t!
(
"{:05d}"
,
-
1
),
"-0001"
);
t!
(
ifm
t!
(
"{:+05d}"
,
1
),
"+0001"
);
t!
(
ifm
t!
(
"{:+05d}"
,
-
1
),
"-0001"
);
t!
(
forma
t!
(
"{:+5d}"
,
1
),
" +1"
);
t!
(
forma
t!
(
"{:+5d}"
,
-
1
),
" -1"
);
t!
(
forma
t!
(
"{:05d}"
,
1
),
"00001"
);
t!
(
forma
t!
(
"{:05d}"
,
-
1
),
"-0001"
);
t!
(
forma
t!
(
"{:+05d}"
,
1
),
"+0001"
);
t!
(
forma
t!
(
"{:+05d}"
,
-
1
),
"-0001"
);
// Some float stuff
t!
(
ifmt!
(
"{:f}"
,
1.0
f
),
"1"
);
t!
(
ifmt!
(
"{:f}"
,
1.0f32
),
"1"
);
t!
(
ifmt!
(
"{:f}"
,
1.0f64
),
"1"
);
t!
(
ifmt!
(
"{:.3f}"
,
1.0
f
),
"1.000"
);
t!
(
ifmt!
(
"{:10.3f}"
,
1.0
f
),
" 1.000"
);
t!
(
ifmt!
(
"{:+10.3f}"
,
1.0
f
),
" +1.000"
);
t!
(
ifmt!
(
"{:+10.3f}"
,
-
1.0
f
),
" -1.000"
);
test_ifmtf
();
t!
(
format!
(
"{:f}"
,
1.0
f
),
"1"
);
t!
(
format!
(
"{:f}"
,
1.0f32
),
"1"
);
t!
(
format!
(
"{:f}"
,
1.0f64
),
"1"
);
t!
(
format!
(
"{:.3f}"
,
1.0
f
),
"1.000"
);
t!
(
format!
(
"{:10.3f}"
,
1.0
f
),
" 1.000"
);
t!
(
format!
(
"{:+10.3f}"
,
1.0
f
),
" +1.000"
);
t!
(
format!
(
"{:+10.3f}"
,
-
1.0
f
),
" -1.000"
);
test_write
();
test_print
();
}
fn
test_ifmtf
()
{
// Basic test to make sure that we can invoke the `write!` macro with an
// io::Writer instance.
fn
test_write
()
{
use
std
::
rt
::
io
::
Decorator
;
use
std
::
rt
::
io
::
mem
::
MemWriter
;
use
std
::
rt
::
io
;
use
std
::
str
;
let
mut
buf
=
MemWriter
::
new
();
ifmtf
!
(
&
mut
buf
as
&
mut
io
::
Writer
,
"{}"
,
3
);
write
!
(
&
mut
buf
as
&
mut
io
::
Writer
,
"{}"
,
3
);
{
let
w
=
&
mut
buf
as
&
mut
io
::
Writer
;
ifmtf!
(
w
,
"{foo}"
,
foo
=
4
);
ifmtf!
(
w
,
"{:s}"
,
"hello"
);
write!
(
w
,
"{foo}"
,
foo
=
4
);
write!
(
w
,
"{:s}"
,
"hello"
);
writeln!
(
w
,
"{}"
,
"line"
);
writeln!
(
w
,
"{foo}"
,
foo
=
"bar"
);
}
let
s
=
str
::
from_bytes_owned
(
buf
.inner
());
t!
(
s
,
"34hello"
);
t!
(
s
,
"34helloline
\n
bar
\n
"
);
}
// Just make sure that the macros are defined, there's not really a lot that we
// can do with them just yet (to test the output)
fn
test_print
()
{
print!
(
1
);
print!
(
"{:?}"
,
~
[
0u8
]);
println!
(
"hello"
);
println!
(
"this is a {}"
,
"test"
);
println!
(
"{foo}"
,
foo
=
"bar"
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录