Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Must one collect to a vector to join?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading