0

My following Rust code

println!(
    "{}\n",
    cavern
        .iter()
        .map(|row| row
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>()
            .join(""))
        .collect::<Vec<_>>()
        .join("\n")
);

Is much shorter in Python:

print("\n".join("".join(map(str, row)) for row in cavern))

or in Haskell:

putStrLn (unlines (map (>>= show) cavern))

One of the main reasons is there's no need to collect the iterators before joining. Is there a way to skip this step in Rust too?

yairchu
  • 23,680
  • 7
  • 69
  • 109

1 Answers1

2

You may use the identically-named join function from the itertools crate. But using collect and join is apparently faster. Also take a look at this answer.

Riwen
  • 4,734
  • 2
  • 19
  • 31