(This came up in today's @rust-lang/lang meeting.)
Given the following code:
fn main() {
let s1 = "a\
b";
let s2 = "a\
b";
println!("{:?} {:/}", s1, s2);
}
Playground link
The compiler interprets both of the string literals as "ab", omitting multiple newlines after the \.
This seems confusing, and different from the behavior of other languages. For instance, C and Python both produce parse errors for such string literals. Or, for a Python multi-line string literal, the backslash escapes the immediately following newline but not the subsequent newline.
Rust does allow unescaped newlines in string literals, which is useful. The ability to escape a newline can also be useful sometimes. But the combination of both escaped and unescaped newlines seems sufficiently confusing and surprising to merit a warning.
We'd ideally like to work towards this being an error. Depending on the presence of existing usage in the ecosystem, that might or might not require an edition.
(This came up in today's @rust-lang/lang meeting.)
Given the following code:
Playground link
The compiler interprets both of the string literals as
"ab", omitting multiple newlines after the\.This seems confusing, and different from the behavior of other languages. For instance, C and Python both produce parse errors for such string literals. Or, for a Python multi-line string literal, the backslash escapes the immediately following newline but not the subsequent newline.
Rust does allow unescaped newlines in string literals, which is useful. The ability to escape a newline can also be useful sometimes. But the combination of both escaped and unescaped newlines seems sufficiently confusing and surprising to merit a warning.
We'd ideally like to work towards this being an error. Depending on the presence of existing usage in the ecosystem, that might or might not require an edition.