我可以创建类型别名来指定 std 容器的分配器吗?

ajp*_*ajp 0 rust

std我想使用自定义分配器为容器定义类型别名,例如

type MyBox<T> = Box<T, A=MyAlloc>;
Run Code Online (Sandbox Code Playgroud)

然而我被告知

error[E0229]: associated type bindings are not allowed here
  --> src/lib.rs:12:24
   |
12 | type MyBox<T> = Box<T, A=MyAlloc>;
   |                        ^^^^^^^^^ associated type not allowed here
Run Code Online (Sandbox Code Playgroud)

为什么这是不允许的,是否有一些解决方法(每晚都可以)?

操场

cdh*_*wie 8

默认类型始终位于泛型类型定义A(此处缺少——您将其用作A泛型类型而不定义它),而不是在使用该类型的地方。这有效:

type MyBox<T, A = MyAlloc> = Box<T, A>;
Run Code Online (Sandbox Code Playgroud)

A如果您根本不需要该参数,则只需直接指定您的分配器类型:

type MyBox<T> = Box<T, MyAlloc>;
Run Code Online (Sandbox Code Playgroud)