String formatting
Creating strings is easy: you just call String::new() and then use a static string or add characters to it. Sometimes, you might want easier access to how the string gets created. If you, for example, want the string to say Hello {user}!, even though you can probably do something such as creating a String with Hello in it, then appending the username and then the exclamation mark, this is not ideal.
This is where the std::fmt module comes in handy, with its format!() macro and all of its formatting options. These options apply to console printing, string formatting, and even buffer writing with write!() and writeln!() macros. You can find the complete guide in the std::fmt module documentation at the standard library documentation, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access