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:
Maximum width of a chain to fit on one line.
- Default value: 60
- Possible values: any positive integer that is less than or equal to the value specified for max_width
- Stable: Yes
By default this option is set as a percentage of
max_width
provided byuse_small_heuristics
, but a value set directly forchain_width
will take precedence.
rustfmt doesn't know about types, so it treats all method calls the same. You can reduce chain_width
manually in rustfmt.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.