rust-lang / rust-lang/rfcs

Allow `self` as an identifier in patterns

Open
#3,684 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-resolve A-syntax T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Proposal

Allow the use of self as an identifier in identifier patterns (foo @ Foo { bar, baz }).

Motivation

As seen in the following search results within the rust-lang/rust repository, there are numerous instances where developers want to destructure a struct in a let statement with self on the right-hand side.

Search results in the rust-lang/rust repository

For example, consider the following code in library/std/src/sys/pal/sgx/net.rs:

fn try_into_inner(self) -> Result<FileDesc, Socket> {
    let Socket { inner, local_addr } = self;
    Arc::try_unwrap(inner).map_err(|inner| Socket { inner, local_addr })
}

If self were allowed as an identifier in identifier patterns, this could be written more concisely as:

fn try_into_inner(self @ Socket { inner, local_addr }) -> Result<FileDesc, Socket> {
    Arc::try_unwrap(inner).map_err(|inner| Socket { inner, local_addr })
}

This is not only more concise but also more natural from a language specification standpoint.

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

The proposal illustrates the use case in library/std/src/sys/pal/sgx/net.rs; start by reviewing the Rust rules for identifier and @ patterns alongside the RFC process. Compare the existing let Socket { inner, local_addr } = self form with the proposed function-parameter form, and consider the language specification and compiler tests needed to establish the accepted syntax.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.