To create a template string in Rust, you can use the format! macro. The format! macro allows you to create a formatted string by specifying the format string and the values to be inserted into the string. Here's an example:
main.rs132 chars5 lines
Output:
main.rs39 chars2 lines
In the example above, we define a name variable with the value "John", an age variable with the value 30, and a message variable that uses the format! macro to create a string that contains the name and age variables in the specified format. The println! macro is then used to print the message variable to the console.
You can also concatenate multiple strings using the + operator:
main.rs133 chars5 lines
Output:
main.rs9 chars2 lines
In the example above, we define a first_name variable with the value "John", a last_name variable with the value "Doe", and a full_name variable that concatenates the first_name and last_name strings using the + operator. The println! macro is then used to print the full_name variable to the console.
gistlibby LogSnag