Format. How can we copy a formatted string containing other values like integers and arrays into a byte vector? In Rust this can be done with the format and concat macros.
While println can write formatted data to the console, we sometimes want to store this output in a String or byte vector. We can use format to create the string, and then copy it.
Example. This Rust program creates some strings containing formatted data. It then copies these strings to a byte vector and prints the vector the console.
Part 1 We use the "format!" macro here. The integer 10 and the array (with 3 values) is inserted into the string literal.
Part 2 If we have only constant values, we can use the concat macro. This creates a string containing the specified parts.
Part 3 A key benefit of format and concat is that we can save the output and copy it. Here we copy the strings to a byte vector.
Summary. With the format and concat macros, we have an alternative to println. With these macros, we can store the resulting String, and use it elsewhere (or later) in our Rust program.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.