你可以调用由特征中实现的另一个特征静态方法的类型实现的特征静态方法吗?例如:
trait SqlTable {
fn table_name() -> String;
fn load(id: i32) -> Something {
...
Self::table_name() // <-- this is not right
...
}
}
现在感谢Chris和Arjan(见下面的评论/答案)
fn main() {
let kiwibank = SqlTable::get_description(15,None::<Account>);
}
trait SqlTable {
fn table_name(_: Option<Self>) -> String;
fn get_description(id: i32, _: Option<Self>) -> String {
println!("Fetching from {} table", SqlTable::table_name(None::<Self>) );
String::from_str("dummy result")
}
}
struct Account {
id: i32,
name: String,
}
impl SqlTable for Account {
fn table_name(_: Option<Account>) -> String { String::from_str("account") }
}
答案 0 :(得分:2)
您必须将Self更改为Sql Table:
trait SqlTable {
fn table_name() -> String;
fn load(id: i32) -> Self {
...
SqlTable::table_name() // <-- this is not right
...
}
}
静态方法总是在像Trait :: some method()之类的特性上调用。错误#6894涵盖了此问题。
答案 1 :(得分:2)
SomeTrait::some_method()
等特征上调用。_: Option<Self>
并将其传递给None::<T>
。请参阅原始问题(截至今天)编译。