-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement dynamic string interpolation #642
Comments
The runtime-fmt crate by @SpaceManiac implements versions of |
I would rather allow |
@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 |
@eddyb :) |
@eddyb you don't have to reveal my entire plan :) |
See also #543. |
Issue by bstrie
Wednesday Nov 06, 2013 at 16:55 GMT
For earlier discussion, see rust-lang/rust#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:
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:...or use method chaining and generics:
(all code examples are just quick sketches, might be overlooking details (and don't worry about naming))
Any thoughts?
The text was updated successfully, but these errors were encountered: