remove_newlines

Function remove_newlines 

Source
pub fn remove_newlines(str: impl AsRef<str>) -> String
Expand description

Removes newline sequences and any surrounding whitespace, replacing them with a single space.

This function is useful for converting multi-line text into a single line while preserving word separation. It collapses multiple lines and adjacent whitespace into one space.

ยงExamples

let text = "Line 1\n  Line 2 \r\n\tLine 3";
let single_line = remove_newlines(text);
assert_eq!(single_line, "Line 1 Line 2 Line 3");

// Example with potentially escaped newline
let text_escaped = "Line A \\\n Line B";
let single_line_escaped = remove_newlines(text_escaped);
assert_eq!(single_line_escaped, "Line A Line B");