Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to force print!/println! to use a Windows new line (CR LF)

Tags:

rust

I am using Rust 1.9 on Windows 10. When playing with some code and comparing the result captured from standard output, I noticed that output uses a Linux line ending 0x0A (10, LF) rather than windows 0x0D 0x0A (13 10, CR LF). I tried following:

println!("{} or {}  = {}", a, b, a | b);

print!("{} or {}  = {}\n", a, b, a | b);

Is there a way to force Windows line endings?

like image 817
Sebastian K Avatar asked Jun 01 '16 02:06

Sebastian K


1 Answers

If you check out the implementation of println!, it's pretty straight-forward:

macro_rules! println {
    ($fmt:expr) => (print!(concat!($fmt, "\n")));
    ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
}

You can copy-paste-modify this to replace \n with \r\n:

macro_rules! wprintln {
    ($fmt:expr) => (print!(concat!($fmt, "\r\n")));
    ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\r\n"), $($arg)*));
}
like image 61
Shepmaster Avatar answered Sep 28 '22 18:09

Shepmaster