-
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
better string interpolation #1250
Comments
We have almost this: https://doc.rust-lang.org/std/fmt/#named-parameters |
Making macro take arguments from the context would make them non-hygienic. |
There are ways to safely allow this kind of hygiene-bending, if we wanted to |
To add more examples in addition to Scala: Python 3.6 introduced a similar interpolation style (so called: literal string interpolation with f-strings), e.g.:
And Ruby also has a similar style - e.g.: |
JavaScript too with template strings:
|
I'm brand new to rust, coming from C#. I love C#'s string interpolation: $"This {stringoutput} is an interpolated string" The difference is the interpolation is activated by the $ preceeding the quote. Inside the {} can be anything that can go on a single line and output a string. I'd love to see something like this in rust. |
Sorry if this is annoying, but it's the same with me coming from Kotlin, which I love:
|
I'd like to be able to write this :
Instead of :
|
This would be very nice, it would also be nice if this supported none string formats in the cases where you now need to do: |
Having strings Rust already checks The good thing is that An open question is how to describe string declaration, e.g. |
@ArturAmpilogov ... and let's not forget: # echo "Hello $NAME $SURNAME" # bash and sh I must confess it astonished me when I started in rust that a modern language did not not support string interpolation. Still surprises me :-) |
I know this issue is old, but string interpolation would make our lives better (more fun and easier). # I prefer
outfile.write(
format!("{x}\t{y}\t{z}\n")
.as_bytes()
)
# over
outfile.write(
format!("{}\t{}\t{}\n", x, y, z)
.as_bytes()
)
# and
outfile.write(
format!("{x}\t{y}\t{z}\n", x=x, y=y, z=z)
.as_bytes()
) The first one is much easier to inspect than the second and third versions. |
Could we write a macro that simulates the third version by typing @tcalvert's version? injected_format!("{x}\t{y}\t{z}\n", x, y, z)
// expands to
format!("{x}\t{y}\t{z}\n", x=x, y=y, z=z) Even if not in the standard, I'd like to know if a user can make his/her own Although I don't like the redundancy in this syntax (why is there injected_format!("{x}\t{y}\t{z}\t{x}\n", z, y, x)
// expands to
format!("{x}\t{y}\t{z}\t{x}\n", z=z, y=y, x=x) also works. Which means if you want to reorder the interpolated variables, you don't have to reorder them at two places. Ideally we would have a macro that parses everything inside the braces and generates the named arguments for them, only once: simple_format!("{x}\t{y}\t{z}\t{x}\n")
// expands to
format!("{x}\t{y}\t{z}\t{x}\n", x=x, y=y, z=z) This can work, because evaluating simple variables should have no secondary effect (unless you write some tricky However, the macro would probably be much more complex, being able to handle cases like brace escaping ( |
It turns out we can write a proc macro to do this now: https://crates.io/crates/ifmt. Handles expressions ( |
Wow. That's pretty cool. I'm still new enough that I'm not sure how macros work, but I assume you just wrapped the basic ones in a new version that parses the string for formatting tags and replaces them? |
This may break backward compatibility. |
+1 for this feature, I'm new to Rust, and I think anything related to string manipulation is of paramount importance. Especially when it comes to making REST APIs or front-end and GUIs |
But would it really? All of the formatting macros require parameters to be passed when the format string contains any let x = 42;
println!("x: {x}"); // error: there is no argument named `x` Since it seems this would only affect code that is currently illegal, it should be trivial to have formatting macros that can handle both the current style and interpolated strings: #[doc(hidden)]
pub use {std::println as _std_println, ifmt::iprintln as _ifmt_println};
#[macro_export]
macro_rules! println2 {
($fmt:expr) => ($crate::_ifmt_println!($fmt));
($fmt:expr, $($args:tt)*) => ($crate::_std_println!($fmt, $($args)*));
} println2!("a number: {42}");
println2!("another number: {}", 360);
|
I think JavaScript should interpolate string as Rust not the other way round , in rust inside {} parenthesis we can do a lot of trick on the interpolation , the only problem with rust interpolation is you have to track the position of the string. or if everybody still needs JS like interpolation then Rust should go with something like the angularjs interpolation {variable | formating} e.g {{ date | date : format : timezone }} |
+1 Kotlin styled string interpolation |
Dup of #2795? |
@lzutao It appears that the PR you linked could be a proposed implementation for this issue. I'm not sure on the "Dup" thing though, since usually PRs that solve an issue are not considered "Dups". Most often people refer to one issue as a Dup of another if they are essentially both the same issue. |
That RFC is only for implicit capture of identifiers, supporting I think this RFC should be used to discuss potential expansions to expression or dotted access interpolation, maybe using the suggested |
Haven't tried yet but with some nightly features i think this exists here: https://github.com/anowell/interpolate |
@benorgera |
This is now stable afaik, should be closed? |
@tyoc213 can you provide some details please? (E,g. a link documenting the current state of the feature would be perfect) |
@AndrewSav probably referring to changes with Rust 1.58 (Jan 2022)? This page details what you can do fairly well. This doesn't allow for expressions within the string (which the discussion here seems to have changed towards, but could be raised as a separate issue), you'd still need to do that externally, but is mostly the same beyond that. |
@rust-lang/lang What do you think about closing this? |
I agree with close -- https://blog.rust-lang.org/2022/01/13/Rust-1.58.0.html#captured-identifiers-in-format-strings covers what was in the OP here. People can always make new IRLO or Zulip threads to discuss potential extensions, though I'm fairly sure it'll never be "any expression", and thus any proposal would need to justify a small-but-useful-enough addition. |
things like:
are akward and verbose. What about scala-like string interpolation that will work everywhere (not only in print statements)
The text was updated successfully, but these errors were encountered: