是否生锈格式!宏提供用户指定的填充字符
Does the rust format! macro provide for user-specified fill characters
使用 Rust,我可以在对 format!
、
的调用中使用用户指定的宽度
format!("{:>width$}", result.clone(), width=v as usize )
我什至可以指定填充字符(如下面的-
)
format!("{:->width$}", result.clone(), width=v as usize )
或者,0
,
format!("{:0>width$}", result.clone(), width=v as usize )
但是有没有办法让用户指定?我已经尝试了以下但它不起作用,
format!("{:fill$>width$}", result.clone(), fill='0' width=v as usize )
我收到以下错误,
error: invalid format string: expected `'}'`, found `'>'`
--> src/sequence/renderer.rs:124:28
|
124 | |v| Ok(format!("{:fill$>width$}", result.clone(), fill='0', width=v as usize ))
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`
error: could not compile `letter-sequence` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed
暂时不能。通过查看 grammar of format string 无法将 fill
参数作为参数传递。
format := '{' [ argument ] [ ':' format_spec ] '}'
argument := integer | identifier
format_spec := [[fill]align][sign]['#']['0'][width]['.' precision]type
fill := character
align := ''
sign := '+' | '-'
width := count
precision := count | '*'
type := '' | '?' | 'x?' | 'X?' | identifier
count := parameter | integer
parameter := argument '$'
如您所见,fill
格式说明符直接映射到 character token,其中 width
可以替换为 identifier $
(width
-> count
-> parameter
-> argument $
-> identifier $
)
您可以使用 runtime-fmt (https://crates.io/crates/runtime-fmt)。这个想法是做这样的事情
!!!未经测试!!!
#[macro_use] extern crate runtime_fmt;
fn main()
{
let result = 400;
let v = 45;
let f = format!("{{:{file}>{width}}}", width=v as usize, file='0');
let s = rt_format!(f, result).unwrap();
println!("{}", s);
}
一些有趣的链接:
使用 Rust,我可以在对 format!
、
format!("{:>width$}", result.clone(), width=v as usize )
我什至可以指定填充字符(如下面的-
)
format!("{:->width$}", result.clone(), width=v as usize )
或者,0
,
format!("{:0>width$}", result.clone(), width=v as usize )
但是有没有办法让用户指定?我已经尝试了以下但它不起作用,
format!("{:fill$>width$}", result.clone(), fill='0' width=v as usize )
我收到以下错误,
error: invalid format string: expected `'}'`, found `'>'`
--> src/sequence/renderer.rs:124:28
|
124 | |v| Ok(format!("{:fill$>width$}", result.clone(), fill='0', width=v as usize ))
| - ^ expected `}` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`
error: could not compile `letter-sequence` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed
暂时不能。通过查看 grammar of format string 无法将 fill
参数作为参数传递。
format := '{' [ argument ] [ ':' format_spec ] '}' argument := integer | identifier format_spec := [[fill]align][sign]['#']['0'][width]['.' precision]type fill := character align := '' sign := '+' | '-' width := count precision := count | '*' type := '' | '?' | 'x?' | 'X?' | identifier count := parameter | integer parameter := argument '$'
如您所见,fill
格式说明符直接映射到 character token,其中 width
可以替换为 identifier $
(width
-> count
-> parameter
-> argument $
-> identifier $
)
您可以使用 runtime-fmt (https://crates.io/crates/runtime-fmt)。这个想法是做这样的事情
!!!未经测试!!!
#[macro_use] extern crate runtime_fmt;
fn main()
{
let result = 400;
let v = 45;
let f = format!("{{:{file}>{width}}}", width=v as usize, file='0');
let s = rt_format!(f, result).unwrap();
println!("{}", s);
}
一些有趣的链接: