Rfcs: Implement dynamic string interpolation

Created on 21 Jan 2015  ·  6Comments  ·  Source: rust-lang/rfcs

Issue by bstrie
_Wednesday Nov 06, 2013 at 16:55 GMT_

_For earlier discussion, see https://github.com/rust-lang/rust/issues/10318_

_This issue was labelled with: B-RFC in the Rust repository_


In a perfect world, a minimal quine in Rust would look like this:

fn main(){let S="fn main(){let S={1}{0}{1};println!(S,S,'{1}')}";println!(S,S,'"')}

Sadly, this is impossible. Why? Because println! requires that the first argument not just be static; it must be a _string literal_.

There are other, non-contrived use cases for dynamic string interpolation; acrichto mentions internationalization as one example.

So what would be involved in implementing dynamic string interpolation?

acrichto mentions (https://botbot.me/mozilla/rust/msg/7556963/) that the current machinery used to parse strings for format! provides an interface that could be reused (std::fmt). But that's only part of the solution.

The first issue is that we can't guarantee type safety for dynamic interpolation (can we???), so the syntax for dynamic format strings might need to be rethought. For now we should probably assume that all arguments to dynamic string interpolation will implement ToStr and just ignore any format string options that aren't for positional or named arguments.

The second issue is that we can't just simply have anything like a simple .format() method on strings, like Python has, since we don't have variadic functions. We'd need to either do something like pass an array of trait objects:

"ab{1}d{0}".format(["e" as ~ToStr, "c" as ~ToStr]);  // gross
dyn_format!("ab{1}d{0}", "e", "c");  // maybe a little less gross

...or use method chaining and generics:

fn push<T: Any>(&mut self, x: T) { self.vec.push(~x as ~Any) }
"ab{1}d{0}".format().push("e").push("c").to_str()  // still kinda gross

(all code examples are just quick sketches, might be overlooking details (and don't worry about naming))

Any thoughts?

T-libs

Most helpful comment

@eddyb you don't have to reveal my entire plan :)

All 6 comments

The runtime-fmt crate by @SpaceManiac implements versions of format! and other interpolation macros that allow dynamic formatting strings.

I would rather allow format_args! to take a constexpr string argument, which would come with better constexpr and less use of macros.

@ubsan You would at least need variadic generics to pull that off, and then you have the named arguments feature... which I suppose you can encode as Named::<"foo">(&expr) for foo = expr.

@eddyb :)

@eddyb you don't have to reveal my entire plan :)

See also #543.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marinintim picture marinintim  ·  3Comments

rudolfschmidt picture rudolfschmidt  ·  3Comments

clarfonthey picture clarfonthey  ·  3Comments

3442853561 picture 3442853561  ·  3Comments

torkleyy picture torkleyy  ·  3Comments