On Windows, a new line is a carriage return (\r) followed by a newline (\n). On most everything else, a new line is just a newline.
On most terminals, including Windows, this will result in two effective newlines as it is written.
Edit: I believe the println call will also add its own newline, which probably gets merged with the carriage return on the console. Nevermind this, early morning brain read bad.
I believe the println call will also add its own newline, which probably gets merged with the carriage return on the console.
Not familiar with Rust, but in a lot of languages the {} is a placeholder, implying r will be placed there, whatever it is. The newline added by the println would then go after that.
I've only ever used one typewriter in my life and you had to do it manually, so I really don't know how common it was for it to do both at the same time
I mean, this is totally valid. The order is a little weird, generally you see \r\n, but some terminals require both... (Windows CMD if I remember correctly? Idk. I never write windows code)
My understanding is that old teletype systems used the carriage return to do exactly that: return the carriage to the initial leftmost position. The line feed would feed the page by one line.
With just a carriage return you'd overtype the current line and with just a line feed you'd continue to type as if you just moved the cursor down.
When TTYs got replaced by video terminals that added many more ways to draw text and allowed you to move a text cursor and not just orient a carriage you could save a byte for every line of text by having your terminal driver assume that a line feed also means an implied carriage return and that you're starting a new line. In some systems (old Apple) the carriage return was the line terminator and a line feed was implied.
Before fancy drivers for things like video terminals if you wanted to do something like underlined text you might do: Hello World\b\b\b\b\b\b\b\b\b\b\b___________\r\n (35 bytes) or Hello World\r___________\r\n (25 bytes and faster) then with output drivers you could get fancier and even more space saving by sending escape sequences to the driver that would be stateful and allowed you to do things like \e[4mHello world\e[0m\n (16 bytes).
"Accept \r\n and \n, output \n" is probably a better rule of thumb tbh. Avoids interesting bugs when you port between nix and windows and use output files from the original.
87
u/cmptrnrd Jan 10 '22
I'm not familiar with rust. Can someone explain what's wrong here? Is it just the weird carriage return?
54
u/lurebat Jan 10 '22
The funny thing is that it wouldn’t matter for an actual typewriter
48
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 10 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
19
u/ewpratten Jan 10 '22
I mean, this is totally valid. The order is a little weird, generally you see \r\n, but some terminals require both... (Windows CMD if I remember correctly? Idk. I never write windows code)
12
u/Likely_not_Eric Jan 10 '22
My understanding is that old teletype systems used the carriage return to do exactly that: return the carriage to the initial leftmost position. The line feed would feed the page by one line.
With just a carriage return you'd overtype the current line and with just a line feed you'd continue to type as if you just moved the cursor down.
When TTYs got replaced by video terminals that added many more ways to draw text and allowed you to move a text cursor and not just orient a carriage you could save a byte for every line of text by having your terminal driver assume that a line feed also means an implied carriage return and that you're starting a new line. In some systems (old Apple) the carriage return was the line terminator and a line feed was implied.
Before fancy drivers for things like video terminals if you wanted to do something like underlined text you might do:
Hello World\b\b\b\b\b\b\b\b\b\b\b___________\r\n(35 bytes) orHello World\r___________\r\n(25 bytes and faster) then with output drivers you could get fancier and even more space saving by sending escape sequences to the driver that would be stateful and allowed you to do things like\e[4mHello world\e[0m\n(16 bytes).5
u/Giocri Jan 10 '22
Always use Environment.newline or whatever the equivalent in your language is to be sure it is always working in whatever environment you are.
1
u/iambored1234_8 Jan 11 '22
Say that to BIOS