Const generics allows constant values, like integers, to be specified as generic arguments on data types. It was first introduced into stable [rust] with version 1.51.
Questions tagged [const-generics]
29 questions
4
votes
1 answer
Does rust support const-generic types with a runtime-determined value?
Consider the classic example of a const-generic datastructure: a square matrix.
struct Matrix {
inner: [[T; N]; N]
}
I'd like to return a structure whose const parameter is dynamically defined:
fn read_matrix() -> ??? {
…

b0fh
- 1,678
- 12
- 28
3
votes
1 answer
Satisfying a trait bound with a const generic expression, is it possible?
I am trying to make use of the currently unstable feature generic_const_exprs to allow the users of my library to know the resulting dimensions of the types they generate.
My use case is much more complex, but I've created a minimal example with a…

codearm
- 185
- 1
- 9
3
votes
0 answers
Initialize array of arrays from iterator
I'm trying to initialize an array of arrays from an iterator using const generics.
The following code serves as an example:
pub fn foo(iter: Iter) -> [[i32; C]; R] {
let mut res: [[MaybeUninit; C]; R] =…

Adam
- 743
- 1
- 6
- 11
2
votes
1 answer
Const context: Create array with const generic length from init function
I'm trying to create a container object containing a const array using a const initializer function for each element.
For arrays of a fixed size (notated with an integer literal) this is already solved; the twist here is that the length of the array…

Finomnis
- 18,094
- 1
- 20
- 27
2
votes
0 answers
Rust equivalent of #Define?
I have a trait that has functions which need to return constantly sized arrays. Since generic array currenly does not support generic expressions within a constant generic, I have had to change certain constants from trait consts to generic consts.…

gormatron3000
- 51
- 1
2
votes
1 answer
Method bounded by a const generic expression does not satisfy trait bound
I'm currently implementing a ray tracer following along the book "The Ray Tracer Challenge" by Jamis Buck.
I've arrived at the part where I have to implement a few methods on matrices, and since these matrices have a compile time known size, I chose…

guimauve
- 408
- 1
- 5
- 12
2
votes
1 answer
How to initialize a const generic array?
I am trying to learn more about const generics and how they can apply to some grid algorithms in any dimensions. Below is a snippet - how can I create an array of the size of a const generic parameter?
type Point = [i32; N];
fn…

adapap
- 137
- 1
- 1
- 5
2
votes
2 answers
Const expressions inside trait?
I have a trait that look like this:
pub trait Buf {
fn to_buf(&self) -> [u8; N];
fn from_buf(buf: [u8; N]) -> Self;
}
However I want to do something like this:
trait Buf {
const N: usize;
fn to_buf(&self) -> [u8;…

Nur
- 2,361
- 2
- 16
- 34
2
votes
2 answers
Implementing a trait for all types implementing a trait with const parameter
I believe that the following code makes sense:
trait FooConst {}
trait Foo {}
impl, const N: usize> Foo for T {}
However when I try to compile it I get error E0207 stating that the parameter N is unbounded. I don't…

JoeyBF
- 123
- 4
2
votes
1 answer
Mismatched types when using associated consts and const generics in Rust nightly
So for a library I am writing, I want to calculate the distance between two points in N dimensions (2, 3, 4, etc...) and I have a Point trait so that user's of the library can use this function on their own types, so long as they are "point like".
I…

scott97
- 75
- 5
1
vote
1 answer
Constrain trait by const generic expression in Rust
I am trying to get a better understanding of const generic expressions and I have not been able run the following
trait Foo {}
impl Foo<{ N * N }> for () {}
because
the const parameter N is not constrained by the…

Saxpy
- 129
- 1
- 7
1
vote
2 answers
How to more easily write !bool in const generics when calling functions?
I have code similar to the following:
fn f1(x:u64) { }
fn f2(x:u64) {
match B {
true => f1::(x),
false => f1::(x),
}
}
That is, I need to negate a const bool in function calls.…
user1002430
1
vote
0 answers
Rust Matrix Type with Specialized and Generic Functions
Given a matrix type in Rust using const generic parameters, something like this:
pub struct Mat
{
m: [[T; C]; R],
}
I am aware that specializations are not available (yet, and not for this type…

Xaldew
- 560
- 5
- 18
1
vote
1 answer
Create "axis" vectors as compile-time constants
Given a rather simple "small-vector" struct used for graphics or linear algebra:
pub struct VecN
{
v: [T; N],
}
How would one create the typical "axis" vectors as compile-time constants?
I.e., something along the line…

Xaldew
- 560
- 5
- 18
1
vote
1 answer
How to create a proc macro that can read a const generic?
I wanted a way to create an array with non-copy values. So I came up with the following:
use proc_macro::TokenStream;
use quote::quote;
use syn::parse::{Parse, ParseStream, Result};
use syn::{parse_macro_input, Expr, LitInt, Token};
struct…
user1002430