rust-lang / rust-lang/rfcs

Closure-level inline constants

Open
#3,687 3 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

I propose to add closure-level inline constants. For example the following code

let closeure_uuid = Uuid::new_v4();
let lambda = ||  {
println!("Execution UUID {}",Uuid::new_v4());
println!("Closure UUID {}",closeure_uuid);
};
lambda ();
lambda ();

It could be rewritten as

let lambda = ||  {
println!("Execution UUID {}",Uuid::new_v4());
println!("Closure UUID {}",cc{Uuid::new_v4())});
};
lambda ();
lambda ();

with a new cc keyword similar to constant. This would have similar advantages as inline_const https://github.com/rust-lang/rust/pull/104087. Use cases:

Finer control on what is getting moved inside the lambda. For example

cc{very_larnge_vector.iter().min()}

or

cc{struct.field.other_filed}

More ergonomic dealing with ref counting. Example:

cc{button.clone()}.enable();

Those expressions should be executed when the closure is created and dropped when the closure is dropped.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked inline_const discussion and the examples in this issue to understand the proposed cc syntax and closure capture behavior. Define the design for creation-time evaluation, closure-lifetime storage, and drop-time cleanup, with agreement on how large expressions and cloned values should behave.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.