Format¶
Formats values into a string using replacement fields.
Module: Std
Summary¶
Description¶
Format creates a new string by replacing placeholder fields in a format string with the string representation of supplied arguments.
Arguments must implement the Display interface.
Each occurrence of {} is replaced by the next argument in the argument list.
Parameters¶
| Parameter | Description |
|---|---|
fmt |
Format string containing zero or more {} placeholders. |
args |
Values used to replace placeholders. |
Return Value¶
Returns a newly allocated String containing the formatted result.
Placeholders¶
The following placeholder is supported:
Each placeholder consumes one argument.
Example¶
Result:
Examples¶
Single Argument¶
Result:
Multiple Arguments¶
Result:
No Placeholders¶
Result:
Using Display¶
Any type implementing Display can be formatted.
struct Person {
name: String;
}
extend Person : Display {
func ToString(self) -> String {
return self.name;
}
}
let person = Person {
name: String::From("Alex")
};
let text = Format("{}", person);
Result:
Notes¶
- Arguments are formatted by calling
ToString(). - Placeholders are processed from left to right.
- Extra arguments are ignored.
- Missing arguments produce no output for the corresponding placeholder.
- A new string is always returned.