Skip to content

Assist: “Extract into variable” on multiple fields of a struct #21863

@kpreid

Description

@kpreid

Starting from the code

struct Foo {
    x: &'static str,
    y: &'static str,
}

fn foo() -> Foo {
    Foo {
        x: "abcdef",
        y: "ghi",
    }
}

it is currently possible to transform foo() into

fn foo() -> Foo {
    let x = "abcdef";
    let y = "ghi";
    Foo { x, y }
}

by putting the cursor on each of the two field values and invoking the “Extract into variable” assist twice. I think it would be useful to offer this same transformation in a way which

  • does not require the cursor to be placed exactly on the value expression (I frequently try to invoke it starting from the field name, before remembering that that doesn’t work), and
  • is able to operate on multiple struct fields at once.

In particular, I propose that it should be possible to extract multiple field value expressions into correspondingly named variables when the cursor is

  • in the struct name, in which case it should act on all fields,
  • in a single field name, or
  • a selection of multiple fields within the struct literal, in which case it should act on all fields whose names or value expressions are within the selection.

In these cases, the rename prompt should possibly be skipped, since it is likely that the user wants the variable(s) to be named the same as the field(s).

@rustbot label +A-assists

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions