# Newline in source code files and strings

**URL:** <https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989>\
**Category:** compiler\
**Created:** [February 5, 2021, 4:56am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989 "2021-02-05T04:56:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![CXH](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cxh/32/7736_2.png) [@CXH](https://internals.rust-lang.org/u/CXH)\
**Post date:** [February 5, 2021, 4:56am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/1 "2021-02-05T04:56:32Z")

</div>

If we want to create a complex string (that is, we need to use the original string, and cannot use the escape character \r\n) and need "CRLF" as the newline character, it does not work to save the source code file as "CRLF". The string always uses "LF" as the newline character

The code is as follows:

```
fn main() {
    let json_str = r#"{
    "name":"zhang_san",
    "age":"13"
}"#;
    //Expected output:
    //"{\r\n \"name\":\"zhang_san\",\r\n \"age\":\"13\"\r\n}"
    //Actual output:
    //"{\n \"name\":\"zhang_san\",\n \"age\":\"13\"\n}"
    println!("{:?}",json_str);
}

```

Does the compiler need to make changes, such as: let the string follow the newline character of the source code, or add an error prompt“ error:couldn't read src\ main.rs : stream did not contain valid LF ", just like" error: couldn't read src\ main.rs : stream did not contain valid UTF-8".

_The above is machine translation, my English is not good_

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [February 5, 2021, 5:31am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/2 "2021-02-05T05:31:52Z")

</div>

That's an unfortunate requirement. I think you can write a macro which inserts CRLF into the strings.

---

<div class="post-metadata">

**Author:** ![CXH](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cxh/32/7736_2.png) [@CXH](https://internals.rust-lang.org/u/CXH)\
**Post date:** [February 5, 2021, 5:50am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/3 "2021-02-05T05:50:30Z")

</div>

Yes, it is actually very simple to achieve the result. But the CRLF string becomes LF when written into the code, which may be a bit forced for me. Maybe it is my own problem. Thank you for your reply.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [February 5, 2021, 6:02am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/4 "2021-02-05T06:02:09Z")

</div>

You can use `include_str!` if you need line endings preserved.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [February 5, 2021, 6:35am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/5 "2021-02-05T06:35:00Z")

</div>

> [@CXH](#):
>
> it does not work to save the source code file as "CRLF". The string always uses "LF" as the newline character

[This is documented here.](https://doc.rust-lang.org/reference/tokens.html#string-literals)

---

<div class="post-metadata">

**Author:** ![CXH](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cxh/32/7736_2.png) [@CXH](https://internals.rust-lang.org/u/CXH)\
**Post date:** [February 6, 2021, 5:45am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/8 "2021-02-06T05:45:17Z")

</div>

Forgive me for another question, why does it do this? What's the benefit of doing this?

In document:

> Both byte sequences are normally translated to `U+000A`

---

<div class="post-metadata">

**Author:** ![birkenfeld](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/birkenfeld/32/1150_2.png) [@birkenfeld](https://internals.rust-lang.org/u/birkenfeld)\
**Post date:** [February 6, 2021, 5:49am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/9 "2021-02-06T05:49:57Z")

</div>

Consistency, so you get the same result no matter where you compile the code.

---

<div class="post-metadata">

**Author:** ![CXH](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cxh/32/7736_2.png) [@CXH](https://internals.rust-lang.org/u/CXH)\
**Post date:** [February 6, 2021, 6:07am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/10 "2021-02-06T06:07:39Z")

</div>

So why not convert to "\r\n"?(This is my last question. I'm stupid.`(_\>﹏\<_)′)

---

<div class="post-metadata">

**Author:** ![birkenfeld](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/birkenfeld/32/1150_2.png) [@birkenfeld](https://internals.rust-lang.org/u/birkenfeld)\
**Post date:** [February 6, 2021, 8:17am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/11 "2021-02-06T08:17:08Z")

</div>

Because they had to choose one convention and chose the simpler one (`\n` vs `\r\n`).

---

<div class="post-metadata">

**Author:** ![CXH](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cxh/32/7736_2.png) [@CXH](https://internals.rust-lang.org/u/CXH)\
**Post date:** [February 6, 2021, 8:39am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/12 "2021-02-06T08:39:44Z")

</div>

all right.╮(╯-╰)╭

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 7, 2021, 8:40am UTC](https://internals.rust-lang.org/t/newline-in-source-code-files-and-strings/13989/13 "2021-05-07T08:40:24Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
