I have a csv file with 14 rows. I iterate over it using fold to convert each row to a single line of text delimited by a comma. I appended the accumulator in the fold which was a mistake but I observed 14 rows takes few seconds to complete and 15 rows hang.
How does 1 more iteration cause a hang?
14 row csv file
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
hello,hello,hello,hello
Code
fn main() {
let mut hello1 = include_str!("../hello1.csv");
hello1.lines().fold(String::new(),|acc,b|{
let delim = match acc.as_str() {
"" => "",
_ => ","
};
let k2 = b.split(",").into_iter().fold(String::new(),|x1,x2|{
// println!("acc={}//",b);
format!("{}{}{} {}",acc,delim,x1,x2)
});
k2
});
}