create a alias type from a const generic type

Viewed 286

warning : this is nighly rust. You need to do this command line: rustup default nightly (and rustup default stable to go back to your previous configurtion)

I Would like to define a type from another type. If I create a type from a const generic type (See 1), it works. But if I create a const generic type from another const generic type, it doesn't work (see 2)

What should I do?

#![feature(const_generics)]

struct Board<T, const WIDTH: usize, const HEIGHT: usize> {
    array: [[T; WIDTH]; HEIGHT],
}

type a = Board<i32,3,3>; // works (1)

type SquareBoard<T, const Dim: usize> = Board<T,WIDTH=Dim,HEIGHT=Dim>;  // doesn't work (2)
1 Answers

I think you do not have to spell out the const parameter names (just as you do not spell out type parameter names):

#![feature(const_generics)]

struct Board<T, const WIDTH: usize, const HEIGHT: usize> {
    array: [[T; WIDTH]; HEIGHT],
}

type a = Board<i32,3,3>; // works (1)

type SquareBoard<T, const Dim: usize> = Board<T, Dim, Dim>;  // works (2)
Related