Skip to content

Commit e36f8ed

Browse files
committed
Clarify why the dedented string always ends with a newline
1 parent b5cdd57 commit e36f8ed

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

text/3830-dedented-string-literals.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,65 @@ fn main() {
11641164
- In the example above, it is not immediately clear where that would be from.
11651165
- It easy to modify the common indentation level of the string in the future, as you do not have to create a new line.
11661166

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+
fn main() {
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+
fn main() {
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+
11671226
### Allowing the closing line to be indented more than previous lines
11681227

11691228
Having the quote be indented further than the first non-whitespace character in the

0 commit comments

Comments
 (0)