Replacing a substring within a string


#1

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!


#2

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