How to go to the next line in a String Rust

Hi,
I have several "\n" in a string, but when I output it, the \n gets outputted, and doesn't go to the next line.
For example:

message += "\n\n\n\n_Unused instances. If you need to start a new instance, consider using one of these types:_\n";

Gives me the output:

\n\n\n\n\n\n\n_Unused instances. If you need to start a new instance, consider using one of these types:_\n

This is just one part of the "message" string. My code essentially parses a JSON file and adds relevant information to "message". There are several \n's throughout the string, but none of them cause line breaks.
Any help would be greatly appreciated. Thanks for your time.

Could you show your code? If you can get a short snippet running in the Playground we'll be able to help you better.

1 Like

Also, what OS are you using? Windows for example uses \r\n.

I am using a Mac.

I'm not sure what else I could show, since it's just a string with "\n" that aren't converting to line breaks.

How are you printing the string? A full runnable example will let us see exactly what's going on.

For example, this program prints the newlines correctly:

fn main() {
    let mut message = String::new();
    message += "\n\n\n\n_Unused instances. If you need to start a new instance, consider using one of these types:_\n";
    println!("{}", message);
}

Output:





_Unused instances. If you need to start a new instance, consider using one of these types:_

(Playground)

What does your code look like?

2 Likes

Oh, I was using the debug: println!("{:?}"). Sorry about that. I tried the normal println!("{}") and it worked. Thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.