官术网_书友最值得收藏!

There's more...

You can combine positional parameters with normal ones, but it's probably not a good idea, as it can quite easily become confusing to look at. The behavior, in this case, is as follows—imagine that format! internally uses a counter to determine which argument is the next to be placed. This counter is increased whenever format! encounters a {} without a position in it. This rule results in the following:

format!("{1} {} {0} {}", "a", "b") // Returns "b a a b"

There are also a ton of extra formatting options if you want to display your data in different formats. {:?} prints the implementation of the Debug trait for the respective argument, often resulting in a more verbose output. {:.*} lets you specify the decimal precision of floating point numbers via the argument, like so:

format!("{:.*}", 2, 1.234567) // Returns "1.23"

For a complete list, visit https://doc.rust-lang.org/std/fmt/.

All of the information in this recipe applies to println! and print! as well, as it is essentially the same macro. The only difference is that println! doesn't return its processed string but instead, well, prints it!

主站蜘蛛池模板: 准格尔旗| 宁阳县| 石楼县| 潍坊市| 合江县| 绥宁县| 同江市| 台安县| 临城县| 民丰县| 桂林市| 讷河市| 佛教| 拜城县| 林州市| 博湖县| 楚雄市| 阿拉善右旗| 潜山县| 吐鲁番市| 叙永县| 长春市| 商水县| 广河县| 兰西县| 勐海县| 乃东县| 五莲县| 新田县| 郴州市| 沅陵县| 南雄市| 龙州县| 鄢陵县| 磐安县| 奇台县| 南木林县| 延边| 宁城县| 肥乡县| 钟山县|