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

后端 未结 5 1999
无人及你
无人及你 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 16:04

    Enum constructors != associated items.

    It is a known issue, but it's not expected to be fixed, at least not in the foreseeable future. From what I have gathered it is not trivial to just allow this to work; at this point it is more likely that the related documentation or the error message will be improved.

    There is little documentation I could find on the topic of associated items in general; The Rust Book has a chapter on associated types, though. In addition, there are plenty of good answers about Self in this related question.

提交回复
热议问题