In the rust rust book, the section on improving with iterators demonstrates putting iterator adapaters on a separate line like so:
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
contents
.lines()
.filter(|line| line.contains(query))
.collect()
}
By default, rustfmt consolidates these to a single line. I cannot find a place in the documentation to prevent this. How can one do so?
Whether method chains are formatted on one line or multiple is primarily governed by the
chain_width
property:rustfmt doesn't know about types, so it treats all method calls the same. You can reduce
chain_width
manually inrustfmt.toml
to encourage it to use new lines more often.That being said, the default width is 60, and the full expression shown here is 62 at least, so I don't know why it would format in a single line for you. When I use rustfmt with the default config it leaves the formatting exactly as shown; on new lines.