Questions tagged [const-generics]

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.

29 questions
1
vote
1 answer

Transmute nested const generic array (Rust)

I want to do pub fn col_vec(mat: [T; DIM0]) -> [[T; 1]; DIM0] { unsafe { std::mem::transmute(mat) } } which is obviously safe to do. Rust rejects it as error[E0512]: cannot transmute between types of different sizes,…
alagris
  • 1,838
  • 16
  • 31
1
vote
0 answers

Enabling feature(generic_const_exprs) being ignored by "cargo build"

I have the following code in src/algebra.rs: #![feature(const_generics, generic_const_exprs)] // Algebra module. struct Polynomial { coefficients: [f64; Order + 1] // f(x) = sum_i=0^Order coefficients[i] * x^i } which is…
quant_dev
  • 6,181
  • 1
  • 34
  • 57
1
vote
0 answers

What is going on with `unconstrained generic constants` in Rust?

I was experimenting with const generics when this strange error came out: error: unconstrained generic constant. What does it mean? Maybe I should describe what was I tried to do before actual code - I wanted to treat types as numbers, using…
Doubtful
  • 83
  • 5
0
votes
1 answer

Define const generics in the where clause

Is there any way to define a const generic in the where clause, currently it doesn't seem to work. If there isn't, are there any plans of adding it in the future? My Failed Attempts fn foo() where const N:usize, {} fn foo() where …
CodeDude
  • 67
  • 5
0
votes
1 answer

Workaround for no const generics in expressions

I'm struggling to work around the limitation that in Rust, you cannot use a const generic parameter in an expression to specify a type. I am still learning rust and am trying to make a matrix type where I calculate the determinant using Laplace…
Rotartsi
  • 527
  • 5
  • 19
0
votes
0 answers

How can I call complex_eigenvalues on a generically sized nalgebra matrix?

I'm using Rust 1.65.0 and nalgebra 0.31.3. Following this blog post, I would like to write a generic function to compute roots of polynomials, without using dynamic memory allocation: use nalgebra::{Complex, Const, DimSub, SMatrix, ToTypenum}; fn…
Sam Estep
  • 12,974
  • 2
  • 37
  • 75
0
votes
0 answers

How to write idiomatic generic bounds with the typenum crate?

I would like to create an array with size bounded by a const expression that uses a generic parameter. In rust pseudo code: struct ArrayWrapper { inner: [T; 2 * (U + 1)] } It seems U would also have to be bound with…
PrancingCrabulon
  • 403
  • 4
  • 15
0
votes
0 answers

"cycle detected when building an abstract representation" with no obvious cycle in Rust

I am exploring the use of const generics in Rust. I have constructed the following toy example, which I believe should compile. #![feature(generic_const_exprs)] trait A { const N: usize; } trait B: A { fn f() -> [bool; Self::N]; } struct…
deaton.dg
  • 1,282
  • 9
  • 21
0
votes
2 answers

Taming large sets of Rust generic parameters

I have a trait that does work on data which is bounded in several dimensions, so I create it with const generics allowing me to define those bounds: trait Transmogrifier { // ... } Then, I have…
womble
  • 12,033
  • 5
  • 52
  • 66
0
votes
1 answer

Is it possible to instantiate new types using const generics in Rust?

I am no expert in const generics but I've tried a couple of different approaches, all with issues, when trying out new type instantiations that involve operating on const generics, for example: when trying to increment base in this const generic…
RequireKeys
  • 466
  • 4
  • 15
0
votes
1 answer

Why is Y, a constrained const generic, reported as unconstrained here?

I’m writing a fixed-size bit sequence type in Rust with the nightly features generic_const_exprs and int_roundings, and I’m able to impl BitAndAssign as such (I’ve not included the body because it isn’t necessary to verify that the first example…
0
votes
1 answer

Is it possible to use functions on Rust's constant generics

So say I'm writing a wrapper type for the array. struct Array { raw: [T;L] } And I have some function that mutates the length of the array wrapper, say the function is concatenation: impl Array { …
Java Machine
  • 83
  • 1
  • 7
0
votes
1 answer

Map dynamic integers to const integers

I need to adapt the interior length of variants of the same struct according to a runtime parameter. The structs are defined generically and differ only in length. The only solution I could think of is using a match statement: pub struct Foo
Adam
  • 743
  • 1
  • 6
  • 11
0
votes
0 answers

Enum for all variations of a const generic struct

With const generics almost here I was wondering how would one build an enum for all (or at least for many) possible variations of a const generic struct? Or if that's not possible, how would one crate a vector that's capable of containing multiple…
Adam
  • 743
  • 1
  • 6
  • 11
1
2