I have written a function to prompt for input and return the result. In this version the returned string includes a trailing newline from the user. I would like to return th
A cross-platform way of stripping a single trailing newline without reallocating the string is this:
fn trim_newline(s: &mut String) {
if s.ends_with('\n') {
s.pop();
if s.ends_with('\r') {
s.pop();
}
}
}
This will strip either "\n" or "\r\n" from the end of the string, but no additional whitespace.