I have a file called a_file.txt
and i want to read it like so and filter out the string "help me"
from each line in that file:
fn main() -> std::io::Result<()> {
let _s: String = std::fs::read_to_string("a_file.txt")?
.lines()
.filter(|line| line.contains("help me"))
.map(|s| s.to_string())
.collect();
Ok(())
}
The map
function here makes it so that collect()
returns a String
not a &str
.
This code returns the text but it seems to have removed the \n
. I assume this happens because the lines()
function consumes the \n
to make an iterator.
How do I retain the \n
s? I want to remove the specified string from each line so I have to use the lines()
function.