Why can't `Self` be used to refer to an enum's variant in a method body?

后端 未结 5 2002
无人及你
无人及你 2020-12-11 15:20

This question is now obsolete because this feature has been implemented. Related answer.


The following Rust code fails to compile:

enum Foo {
          


        
5条回答
  •  情书的邮戳
    2020-12-11 15:51

    If the enum name Foo is in reality long and you want to avoid repeating it across the implementation, you have two options:

    • use LongEnumName as Short at module level. This will allow you to return Short::Bar at the end of f.
    • use LongEnumName::* at module level, allowing an even shorter Bar.

    If you omit pub, the imports will be internal and won't affect the public API of the module.

提交回复
热议问题