Skip to content

Combining openings and closings #61

@joshtriplett

Description

@joshtriplett

(Suggestions welcome for a better title.)

This arose from a discussion in a style-team meeting, as well as discussions in #35.

A generalized rule that may apply to structs, function calls, closures, match arms, and similar: If you have an outer construct that takes a single expression parameter (a struct with a single field, a function with a single parameter...), and that single expression in turn consists of a construct containing a list that needs breaking across multiple lines, you can break the inner list across multiple lines without first breaking the outer thing across multiple lines.

Concretely:

foo(bar(
    param1,
    param2,
    param3,
));

foo(Bar {
    x: value,
    y: value2,
});

let opt = Some(Struct(
    long_argument_one,
    long_argument_two,
    long_argument_three,
));

do_thing(|param| {
    action();
    foo(param)
});

// at the end of some block
Ok(some_function(
    long_argument_one,
    long_argument_two,
    long_argument_three,
))

I've seen this pattern extensively in Rust code.

This may nest further, as long as all but the innermost construct have only a single argument:

Ok(bar(baz(
    param,
    param2,
)));

This should never apply if the outer construct contains more than just the single expression; for instance:

// don't do this
let thing = Struct(first, OtherStruct(
    second,
    third,
));

// don't do this either
foo(thing, bar(
    param1,
    param2,
    param3,
));

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions