Skip to content

format!() should support the same set of \-escapes that regular string literals do #9970

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

Closed
lilyball opened this issue Oct 20, 2013 · 4 comments
Labels
A-syntaxext Area: Syntax extensions

Comments

@lilyball
Copy link
Contributor

As long as format!() treats \ as an escape character, it should handle all the same escapes that normal string literals do. This way a raw string literal can be passed to format!() and still use the expected escapes. Right now, attempting to do so:

println!(r#"this is a "string": {}\n"#, "example code");

throws a compiler error:

 error: invalid escape character `n`
println!(r#"this is a "string": {}\n"#, "example code");
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@alexcrichton
Copy link
Member

I personally don't believe that the format-string format should not be conforming to what rust as a language uses as a string format. I believe that if we start recognizing \n as an escape sequence, then it will start to just lead to confusion.

What I do think, however, is that the error message could be improved in this case. It's no clear that the error is coming from the format string parser and not the rust string parser, and I believe that distinction would alleviate the problem.

alexcrichton added a commit to alexcrichton/rust that referenced this issue Nov 6, 2013
There were a few ambiguous error messages which look like they could have
cropped up from either the rust compiler for the format string parser. To
differentiate, the prefix 'invalid format string' is now added in front of all
format string errors.

cc rust-lang#9970
bors added a commit that referenced this issue Nov 6, 2013
There were a few ambiguous error messages which look like they could have
cropped up from either the rust compiler for the format string parser. To
differentiate, the prefix 'invalid format string' is now added in front of all
format string errors.

cc #9970
@engstad
Copy link

engstad commented Mar 10, 2014

Coming from a C/C++ and Ocaml background, rust's behavior is quite confusing. In C:

printf("\\\n"); // Outputs: \<new-line>

And in Ocaml, format-strings are special strings that enforce types of the arguments:

Printf.printf "\\\n"; (* Outputs: \<new-line> *)

In Python:

print("\\\n")

In Rust, we have to write:

print!("\\\\\n"); // Outputs: \<new-line>

Clearly, for format-strings, the escape character ought to be back-slash, but then

format string meaning
{ literal '{'
} literal '}'
\ literal ''
\n new-line
\t tab

Given the goals of Rust 1.0, I think this issue should be resolved before 1.0 is released.

@rprichard
Copy link
Contributor

I think this issue is already fixed -- format! no longer uses \ as an escape character. Instead, {{ and }} are escape sequences.

@alexcrichton
Copy link
Member

I believe so as well, thanks @rprichard!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

4 participants