Calling trait static method from another static method (rust)

若如初见. 提交于 2019-11-30 23:07:53

You have to change Self to SqlTable:

trait SqlTable {
  fn table_name() -> String;

  fn load(id: i32) -> Self {
    ...
    SqlTable::table_name()    // <-- this is not right
    ...
  }
}

Static methods are always called on a trait like SomeTrait::some_method(). Bug #6894 covers this issue.

Ant Manelope
  • Yes, you can call a trait static method [implemented by types] from another trait static method [implemented in the trait].
  • Static methods are always called on a trait like SomeTrait::some_method().
  • Where there is no Self or self in [a trait] function signature, it is not callable at present. The standard workaround until UFCS comes is to take an argument _: Option<Self> and pass it None::<T>.

See original question for code that (as of today) compiles.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!