Skip to content

Code becomes very unclear on mix of for loop and iterator combinators #5148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
masklinn opened this issue Dec 23, 2021 · 1 comment
Closed

Comments

@masklinn
Copy link

The code is basically a longer version of Clippy's manual flatten example, with the iterable inline. If the for line is long enough:

    let an_item = None;
    let an_other_item = None;

    for mut a_nice_declarative_name in [
        Some("a value"),
        an_item,
        Some("an other value"),
        an_other_item,
        Some("more"),
        Some("stuff"),
        Some("again"),
    ].into_iter().flatten() {
        println!("{}", a_nice_declarative_name);
    }

gets reformatted to

    for mut a_nice_declarative_name in [
        Some("a value"),
        an_item,
        Some("an other value"),
        an_other_item,
        Some("more"),
        Some("stuff"),
        Some("again"),
    ]
    .into_iter()
    .flatten()
    {
        println!("{}", a_nice_declarative_name);
    }

the lone block brackets, and methods floating in the middle, feel and read very odd. I actually spent a few seconds wondering why I had put the actual code (whose body is quite a bit more complicated) inside its own block after rustfmt ran on it, interpreting it as a sub-scope rather than what it is (a loop body).

@calebcartwright
Copy link
Member

Thanks for sharing your perspective but I'm going to close because this is correct, expected formatting per the governing Style Guide.

You may be interested in reviewing issues like #4306 (comment) which outlines some (non-default) options we're exploring that would give you the flexibility to maintain the type of formatting you'd authored

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants