anowell / interpolate

Very simple Rust string interpolation
MIT License
36 stars 3 forks source link

How to escape dollar sign? #3

Closed Boscop closed 6 years ago

Boscop commented 6 years ago

How to escape the dollar sign in a string like

let price_usd = 9.99;
let price_str = s!("Total: $ $price_usd");

so that it becomes "Total: $ 9.99"?

anowell commented 6 years ago

Good catch. Escaping isn't currently supported. If sticking with $ syntax, I'd say $$ is probably the right choice.

If I switch to {foo} syntax as mentioned in #2, then I'd probably follow Rust's format string escaping rule:

The literal characters { and } may be included in a string by preceding them with the same character. For example, the { character is escaped with {{ and the } character is escaped with }}.

so s!("{foo}") displays the variable foo and s!("{{foo}}") displays the string "{foo}"

anowell commented 6 years ago

I've added escaping to the 0.2.0 version which using the "{foo}" syntax and "{{foo}}" escaping.

I can revisit dollar-sign escaping if I revert to the $foo syntax.