- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 271字
- 2021-07-02 23:07:21
Positioning the output
One of the more useful extensions in C# is string.Format(...);. This allows for a string to be constructed based on parameters at particular positions. For example, the following statement constructs a string where the parameter at a position after the string literal is inserted into the string (here, the letter B is inserted twice in the middle of the string and then at the end):
var myString = string.Format("Hello {0}, I am a {1}{1}{2} computer model {1}", name, "B", "C");
Rust also supports this form, but with the difference that the positioning may be omitted.
Consider the following examples:
format!("{} {}", 2, 10); // output 2 10 format!("{1} {} {0} {}", "B", "A");
The first example is what we've seen before. The format string gets filled with the parameters on the right, in order.
In the second example, it would seem that we're asking for four parameters, but have only supplied two. The way this works is that the positional arguments are ignored when filling in the non-positional arguments. Indexing, as is usual in programming, starts at zero. This is how the arguments are processed:
- {1} inserts the second parameter A
- {} inserts the first parameter B
- {0} inserts the first parameter B
- {} inserts the second parameter A
Therefore, the output is going to be A B B A.
The following are the two important rules governing the positional parameters:
- All of the arguments within the quotes must be used. Failure to do so will result in a compiler error.
- You can refer to the same argument as many times as you like within the format string.
- DB2 V9權威指南
- 新編Visual Basic程序設計上機實驗教程
- Android項目開發入門教程
- Hands-On Image Processing with Python
- FFmpeg入門詳解:音視頻流媒體播放器原理及應用
- Magento 1.8 Development Cookbook
- Bootstrap 4:Responsive Web Design
- SQL Server 2016數據庫應用與開發
- 單片機應用與調試項目教程(C語言版)
- Python極簡講義:一本書入門數據分析與機器學習
- Frank Kane's Taming Big Data with Apache Spark and Python
- ExtJS Web應用程序開發指南第2版
- C語言程序設計
- C語言程序設計實踐
- Hacking Android