创建格式输出并用 n
填充输出你可以在 Rust 中做这样的事情:
fn main() {
let title = " Title ";
println!("┌{:─^11}┐", title);
}
这将打印:┌── Title ──┐
所以 str 在两边用 ^
填充(通过 -
居中)在 11 个字符的空间内。我怎样才能使这个宽度动态化?通过一个变量。
最佳答案
所以事实证明这是一个内置功能。 std::format
自带 width
选项用后缀表示 $
.
fn main() {
let title = " Title ";
println!("┌{:─^width$}┐", title, width = 11);
}
这将打印:┌── Title ──┐
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5666cd7f274d436e2216e7ecd0320072
关于rust - 如何使 std::format 中的填充填充动态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69067436/