Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a format spec that will output a OS-specific newline?

Tags:

format

rust

In format!(..), print!(..), println!(..) etc. it's easy enough to insert values in various formats, however the docs show no way of adding an OS specific newline (e.g. '\n' in unices/MacOS X, "\r\n" on Windows). Is there such a format specifier? If not, how should one put newlines in their formatted strings/outputs?

like image 741
llogiq Avatar asked Nov 23 '15 09:11

llogiq


1 Answers

I don't believe so.

That said, with the exception of Notepad, just about everything on Windows has long since learned that \n is as much a newline as \r\n. In fact, the only other time I have line ending issues is badly ported UNIX software that refuses to believe in the existence of \r\n newlines.

In other words, unless you have a specific reason for doing so, just stick to \n.

If you do have a specific reason, it's probably simplest to define a NL constant somewhere (I couldn't find one in the stdlib):

#[cfg(windows)] pub const NL: &'static str = "\r\n";
#[cfg(not(windows))] pub const NL: &'static str = "\n";
like image 194
DK. Avatar answered Nov 15 '22 10:11

DK.