rust-lang / rust-lang/rfcs

Generic consts

Open
#2,424 9 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Not to be confused with const generics, but I'll use both to describe the idea.

Let's say you have a const initializer and you want it to be different for every type:

const NONE_T_8ARRAY<T>: [Option<T>; 8] = [None; 8];

This is the basic generic const construct. Ofc, if you want length also specified:

const NONE_T_ARRAY<T, const L: usize>: [Option<T>; L] = [None; L];

This creates an array of size L containing only None.

Ofc, you also don't need to use types, consts are fine too:

const SQUARE<const X: usize>: usize = X * X;

But perhaps you want it to be recursive:

const POW<const X: usize, 0>: usize = 1;
const POW<const X: usize, const Y: usize>: usize = X * POW<X, Y-1>;

And you want to calculate Pi with it [1]:

const POW<const X: usize, 0>: usize = 1;
const POW<const X: usize, const Y: usize>: usize = X * POW<X, Y-1>;
const PI<-1>: f64 = 0;
const PI<const N: isize>: f64 = (1.0/POW<16, N as usize> * (4.0/(8*N + 1.0) - 2.0/(8*N + 4.0) - 1.0/(8*N + 5.0) - 1.0/(8*N + 6.0))) + PI<N-1>;

And things of the sort.

Yeah... they'd be nice.

May we have them?

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 reviewing the proposed generic const examples and the linked C++ template-metaprogramming reference. The issue does not identify files, tests, an implementation entry point, or agreed acceptance criteria; the work would first need a concrete RFC design and a clear definition of what support is complete.

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
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.