rust-lang / rust-lang/rfcs

Feature Request: Allow enums to have const tagged values

Open
#3,303 0 comments 7 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

This may seem like a small deal, and it may only amount to "This seems like something Rust should have."

Although Rust allows enums to have isized tagged values, like so...

enum MyEnum {
    A = 123,
    B = 456,
}

It seems/feels like Rust should also allow const values, such that when I request D1 or D2, I get back A or B respectively:

pub enum C {
    C1,
    C2,
}

const A: &[C] = [C::C1, C::C2].as_slice();
const B: &[C] = [C::C1].as_slice();

pub enum D {
    D1 = A,
    D2 = B,
}

Instead one must implement a get_value() function like so:

impl D {
    pub fn get_value(&self) -> &[C] {
        match self {
            D1 => A,
            D2 => B,
        }
    }
}

I have an example in my own code as well. See for more discussion: https://stackoverflow.com/questions/36928569/how-can-i-create-enums-with-constant-values-in-rust

Thanks!

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 examining the proposed enum examples and the linked Stack Overflow discussion to clarify the intended semantics of const-valued variants. Then review the Rust RFC process in this repository. Done means producing a sufficiently specified proposal that addresses the syntax, supported value types, and how the values are retrieved.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.