rust-lang / rust-lang/rust-analyzer
Assist idea: `Extract into closure`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
Motivation
Sometimes it is desirable to extract a repeated part inside a function into a closure. A standalone function may not be viable due to a lot of externally computed state.
Suggestion
A new assist Extract into closure which takes the currently selected part and puts it into a closure stored in a local variable.
Questions
- How does one determine what should be an argument and what should be captured instead?
- If we just say that we just follow function chains, such that the selected part gets into the closure and the rest of the expression it acts on is passed as argument, then this doesn't work for composed functions
Example
Before assist
struct Existence {
things: Vec<&'static str>,
cats: Vec<&'static str>,
}
impl Existence {
fn important_count(&self) -> usize {
let hyper_complex_outside_state = 12;
let another_state = 2;
let numbers = self
.things
.iter()
.map(|thing| thing.len() * another_state)
.filter(|len| *len <= hyper_complex_outside_state)
.collect::<Vec<_>>();
// TODO: also perform important operation on cats and add it onto here
numbers.len()
}
}
fn main() {
let exist = Existence {
things: vec!["banana", "starfruit", "options", "cats", "tautology"],
cats: vec!["Luna", "Luna", "Luna", "actually i don't even know who this cat is help"],
};
dbg!(exist.important_count());
}
Selected is this part:
.iter()
.map(|thing| thing.len() * another_state)
.filter(|len| *len <= hyper_complex_outside_state)
.collect::<Vec<_>>()
After assist
struct Existence {
things: Vec<&'static str>,
cats: Vec<&'static str>,
}
impl Existence {
fn important_count(&self) -> usize {
let hyper_complex_outside_state = 12;
let another_state = 2;
let closure_name = |elems: &[&str]| {
elems
.iter()
.map(|thing| thing.len() * another_state)
.filter(|len| *len <= hyper_complex_outside_state)
.collect::<Vec<_>>()
};
let numbers = closure_name(&self.things);
// TODO: also perform important operation on cats and add it onto here
numbers.len()
}
}
fn main() {
let exist = Existence {
things: vec!["banana", "starfruit", "options", "cats", "tautology"],
cats: vec![
"Luna",
"Luna",
"Luna",
"actually i don't even know who this cat is help",
],
};
dbg!(exist.important_count());
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation files, tests, or entry points are named. Start with the Questions section to resolve how captured values differ from arguments, then use the Before/After example as the acceptance target and add coverage once the assist's semantics are settled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100