3 ms·
`{}` has always been special in format strings, so no real change there. If you've got a string that happens to contain "Hello {x}!" and you want to print it,
by andrewaylett 5y ago
`{}` has always been special in format strings, so no real change there. If you've got a string that happens to contain "Hello {x}!" and you want to print it, you must template it in, as the format string needs to be a literal:
print!("{}", hello_string)
Or as others have pointed out, you can double-up the braces:
print!("Hello {{x}}!")
Then if you want to be cute, you could do something like this:
print!("Hello {x}!", x="{x}")
Note that the named parameter syntax isn't new -- what's new is capturing named parameters from a scope that's larger than the format macro:
let x = "{x}"
print!("Hello {x}!")
And also note that it's a compile-time error to have an unused format parameter in a string, so that last code example wouldn't have compiled at all in older versions of rust.