I am looking at examples but they all work with only one line piped/input into my rust program, but I need 2 lines or more as Strings so I can remove the \r\n or \n or \r and the last . line, so basically I need them in Strings so I can work on them. so here is some of my snips.
let mut buffer = "".to_string();
io::stdin().read_to_string(&mut buffer)?;
print!("{}",remove(buffer));
// remove() is the fn where I am trying to work with the Strings
...
Above I am reading everything I pipe to my program from stdin. cat test.txt > cargo run that works.. but I am pipping this input:
ClientAuthname: user\r\n
ClientPassword: pass\r\n
.\r\n
so I have made and change like 20 something functions under remove() and cant seem to get it right, rust complainsts about it one way or another, I have tested many examples but they are for u* types or str types etc. when using String mutable or not I always get issues. Some of the solutions I been trying for example this ones Remove single trailing newline from String without cloning
I can't get anything to work with my case. I will appreciate if someone can give me a hand and give a explanation so I learn this since I do a lot of unix systems programming on C/Go and moving my programs to rust.
Thanks [edit] Since people wants me to waste text and post examples of what I tried this are 3 of like 14 or so examples I have tried so far
/*
fn remove(arg: String) -> String {
let newarg = arg.trim_end().to_string();
return newarg;
}
*/
/*
fn parse(s: &mut String) -> String {
if s.ends_with('\n') {
s.pop();
if s.ends_with('\r') {
s.pop();
}
}
return s.to_string();
}
*/
fn parse(s: &mut String) -> String {
s.strip_suffix("\r\n").or(s.strip_suffix("\n")).unwrap_or(s);
return s.to_string();
}
none works. they all fail to remove \r\n or \n :(
so what I have as stdin? I tried everything I see! all of this! https://doc.rust-lang.org/stable/std/io/struct.Stdin.html#method.read_line also I tried all of this! How to capture the output of a process piped into a Rust program?
I can keep going! this is C is super simple to do. dont know why I cant get the output from another program the native unix way wit ha simple pipe, like echo "Password\r\n" | cargo run and remove the "\r\n" nothing works if it comes from stdin from another program.
use std::io;
let lines = io::stdin().lines();
for line in lines {
println!("got a line: {}", line.unwrap());
}
WHY I cant add parse() to the line!!! String and get the \r\n remove with ANY of the examples I find! I change both, the stdin ways and the function to remove, I tried .trim() I tried .pop() I tried .truncate() I tried .strip_suffix("\r\n") !!! everything ! cant someone just write a small snipped that will do exactly what I need since there does not exists any thing that works. ? or at least tell me how I should do it so I do not keep trying and trying and trying everything I see. I am so close to ditch all 1000+ of working code because of something so simple as grabbing and manipulating String from another unix program, that I have done in C thousands of times since Unix works based on pipes.