gpt4 book ai didi

rust - 如何使 std::format 中的填充填充动态?

转载 作者:行者123 更新时间:2023-12-04 12:21:34 25 4
gpt4 key购买 nike

创建格式输出并用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com