Skip to content

Request: Refactor option to extract a non-capture closure into a function #8811

Closed
@KSXGitHub

Description

@KSXGitHub

Given the following code:

numbers // Vec<i32>
    .into_iter()
    .filter(|x| *x % 2 == 0)
    .collect::<Vec<_>>()

In VS Code, I want to select the predicate closure (|x| x % 2 == 0), click the yellow light bulb, and choose "Extract closure into function", and the code should turn into this:

numbers // Vec<i32>
    .into_iter()
    .filter(predicate)
    .collect::<Vec<_>>()

fn predicate(x: &i32) -> bool {
    *x % 2 == 0
}

Note: The name predicate was deduced from the signature of the filter method. However, I wouldn't complain if rust-analyzer instead asks me for the name of the function.

Note: Currently, an option called "Extract into function" exists, but it only moves a whole expression to a new function. In regard to closure, the new function it creates is a function that returns closure, not the closure itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-assistsS-actionableSomeone could pick this issue up and work on it right now

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions