Hello
I'm learning Rust and trying to re-write "head" for this learning experience.
I have noticed that GNU coreutils "head" is passing around the special character ^M.
And in my case I'm using println!() macro to output the lines from input files, also println!() seems to be stripping out ^M from the output or the way I'm reading the file...
I'm curious how I could pass out this character through println().
This is the piece of code where I read the file and output through println!() the lines.
fn open(filename: &str) -> MyResult<Box<dyn BufRead>> {
match filename {
"-" => Ok(Box::new(BufReader::new(io::stdin()))),
_ => Ok(Box::new(BufReader::new(File::open(filename)?))),
}
}
match open(&filename) {
Err(err) => eprintln!("{}:{}", filename, err),
Ok(file) => {
let max_output_line = &config.lines;
let index = &config.files.iter().position(|r| r == filename).unwrap();
let number_of_files = &config.files.len();
let first_file: usize = 0;
if *number_of_files > 1 {
if index == &first_file {
println!("==> {} <==", &filename);
} else {
println!("\n==> {} <==", &filename);
}
}
for (line_num, line) in file.lines().enumerate(){
let line = line?;
if &line_num == max_output_line {
break;
}
println!("{}",line);
}
}
}
For reference here is my input:
In the output I don't have the ^M anymore
PS: ^M is the Windows carriage return character (0xD) as interpreted by VIM when showing all characters.
Thanks in advance for any advice or help.