python - Rust 相当于 Python 的 ljust() 字符串方法
问题描述
我需要&str
在 Rust 中左对齐 a。
在 Python 中,我会这样做:
f"{spam}: {eggs}".ljust(curses.COLS - 1)
我如何在 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。”
推荐阅读
- powershell - WIQL 仅选择 System.* 列(类似于 SQL [select [table].* from [table]])
- c++ - 计数操作 - 嵌套循环中有多少个运算符?
- flutter - 如何在 Flutter 的 showSearch 的 SearchDelegate 中访问 BLoC
- java - 我想在共享按钮单击先发送图像而不是文本时分别发送图像和文本
- python - 在 numba 中,array(float64, 1d, C) 和 array(float64, 1d, A) 有什么区别?
- cassandra - 如何从故障检测器中删除 cassandra 节点
- python-3.x - 如何使用 Boto 3 显示 EC2 实例名称
- jquery - 我可以更改画布上图像对象的样式属性吗?
- r - 当列更改值时对数据框进行子集
- shopify - [Shopify]如何更改搜索结果的列表顺序?