Rust 中的类型别名

Type Aliases in Rust

类型别名在 Rust 中究竟是如何工作的?

我一直在检查一些我没有写的旧 Rust 代码中的破损,并注意到 Thunk::new(...) 导致了这个错误:

  error: type `Box<alloc::boxed::FnBox<_, Output=_> + Send>` 
  does not  implement any method in scope named `new`

Thunk 定义为:

type Thunk<'a, A = (), R = ()> = Box<FnBox<A, Output=R> + Send + 'a>;

我认为 Alias::method 在以前的 Rust 版本中不起作用?我应该如何将 Thunk::new 更改为有效的东西?它是否缺少 Box 或其他内容的导入?

Thunk::new 曾经工作因为它 used to be a struct rather than a type alias. This was changed two days ago: Add (unstable) FnBox trait as a nicer replacement for Thunk.

要解决此问题,请将 Thunk::new 替换为 Box::new,该 PR 中的整个标准库也是如此。同时将 thunk.invoke() 更改为 thunk().