我需要&str在 Rust 中左对齐 a 。
在 Python 中,我会这样做:
f"{spam}: {eggs}".ljust(curses.COLS - 1)
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 Rust 中惯用地做到这一点?
格式化宏,例如println!,write!并format!提供此功能。看看https://doc.rust-lang.org/std/fmt/#fillalignment和https://doc.rust-lang.org/std/fmt/#width。
{:5} 指定最小宽度为 5{:-<5} 左对齐到五位数并用 -{1:0$}:“宽度的值也可以通过添加后缀 $ 来作为参数列表中的 usize 提供,表明第二个参数是指定宽度的 usize。”