You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/3830-dedented-string-literals.md
+59Lines changed: 59 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1164,6 +1164,65 @@ fn main() {
1164
1164
- In the example above, it is not immediately clear where that would be from.
1165
1165
- It easy to modify the common indentation level of the string in the future, as you do not have to create a new line.
1166
1166
1167
+
### The choice of not ending with a newline
1168
+
1169
+
Dedented string literals do not end with a newline.
1170
+
1171
+
The following:
1172
+
1173
+
```rs
1174
+
fnmain() {
1175
+
print!(
1176
+
d"
1177
+
create table student(
1178
+
id int primary key,
1179
+
name text
1180
+
)
1181
+
"
1182
+
);
1183
+
}
1184
+
```
1185
+
1186
+
Prints, *without* a newline at the end:
1187
+
1188
+
```sh
1189
+
create table student(
1190
+
id int primary key,
1191
+
name text
1192
+
)
1193
+
```
1194
+
1195
+
In order to add a final newline, an extra blank line needs to be added at the end:
1196
+
1197
+
```rs
1198
+
fnmain() {
1199
+
print!(
1200
+
d"
1201
+
create table student(
1202
+
id int primary key,
1203
+
name text
1204
+
)
1205
+
1206
+
"
1207
+
);
1208
+
}
1209
+
```
1210
+
1211
+
Removing the final newline is consistent with removing the initial newline.
1212
+
1213
+
The line containing the opening quote `"` and the line containing the closing quote `"` can be considered to be fully exempt from the output.
1214
+
1215
+
If this *wasn't* the behaviour:
1216
+
- It would make less sense to remove the newline from the beginning, but not from the end.
1217
+
- Dedented strings would always end with a newline
1218
+
- ..But how do you opt-out of the newline?
1219
+
1220
+
Using a special syntax, like closing with a `-"` (as a different RFC proposes) would be too special-cased, it wouldn't fit in with the rest of the language.
1221
+
1222
+
It would be confusing for those that want to end the dedented string with a `-`.
1223
+
1224
+
Removing *both* the newline at the start and the end is consistent, and allows maximum flexibility whilst not making additional trade-offs such as having to introduce new special syntax to exclude the newline.
1225
+
1167
1226
### Allowing the closing line to be indented more than previous lines
1168
1227
1169
1228
Having the quote be indented further than the first non-whitespace character in the
0 commit comments