Replacing a substring within a string

Can anyone tell me how to format this string to replace the "test" inside the {} ?

let string = format!(r#"datadoc @> '{"tags": ["{}"]}'"#, "test");

because I'm getting:

error: invalid format string: expected '}', found '"'`


Explanation:

Normally, I can do:

let string = format!("{}{}", "something", "something");

but, however, the string I need for format has double-quotes and curly brackets contained within it. So how I can I resolve this? Or, any docs to read regarding how Rust handles escape characters?

Thanks!

Yes, in the formatting documentation, under "Escaping". You escape { and } as {{ and }}.

2 Likes