Hey yall
I have the following bit of code from a current project. It works if I change const SCALING
to let scaling
. Two questions:
- Is rust not able to do const computations with const generics?
- Can anyone explain me the error message? It's kind of confusing in this context and the explanations seem to point to the case where you try to reuse a generic as a type in the definition of an inner function or type.
type Float = f32;
type SpaceCoordinate = Float;
type GridCoordinate = usize;
const MIN: Float = -1.;
const MAX: Float = 1.;
fn main() {
let a: SpaceCoordinate = 12.;
let b: GridCoordinate = transform::<4>(a);
}
fn transform<const N_GRID: usize>(coord: SpaceCoordinate) -> GridCoordinate {
const SCALING: Float = N_GRID as Float / (MAX - MIN);
((coord * SCALING).floor() - (MIN * SCALING).floor()) as usize
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0401]: can't use generic parameters from outer function
--> src/main.rs:13:28
|
12 | fn transform<const N_GRID: usize>(coord: SpaceCoordinate) -> GridCoordinate {
| ------ const parameter from outer function
13 | const SCALING: Float = N_GRID as Float / (MAX - MIN);
| ^^^^^^ use of generic parameter from outer function
For more information about this error, try `rustc --explain E0401`.
error: could not compile `playground` due to previous error
(E0401)