Rust 中的存在类型

  • Subscription to LWN: Every article on LWN.net is written by humans. They offer a free one-month trial subscription (no credit card required) to keep robots at bay. Link.
  • Rust's work on async code: Contributors have been improving async code support. Since 2017, they've been working on adding explicit existential types.
  • Downsides of type systems: Well-known benefits but also have downsides. Complex type systems can make writing type signatures painful and produce complicated error messages. Examples like SQLAlchemy show this.
  • C++ and Java solutions: C++ and Java adopted auto and var keywords to save programmers from writing long types. But they have their own problems.
  • Rust's approach: Rust has been trying to avoid the trap of complex types. Existential types are a mechanism to make dealing with complex types easier.
  • Existential types today in Rust: Rust has a limited form of existential types called impl Trait types. They are useful for abstracting API details and are used in asynchronous functions and returning closure types. In 2018, they could be function arguments, and in 2023, they could be return values from trait methods.
  • Limitations of impl Trait types: Every occurrence in the program refers to a different concrete type. There is a workaround using trait objects, but it has performance and expressiveness issues.
  • Future of existential types in Rust: Allowing impl Trait types in type aliases would make their use more consistent. This would allow functions to return values of the same type and store them in structures. This is the last major step towards full existential types.
  • Work on existential types: There are still design questions. Associated type defaults are in development and how they interact with existential type aliases is unclear. The road to full existential types has been long but likely to be sorted out soon. It would make writing asynchronous functions more ergonomic. Polishing Rust's async programming story is a roadmap goal for Rust 2024.
阅读 8
0 条评论