Rust希望此UFCS调用使用什么类型的注释?
对不起,我可能错过了一些明显的东西。我想知道为什么我不能这样称呼我的特质方法。这不应该是标准的UFC吗Rust希望此UFCS调用使用什么类型的注释?,rust,traits,Rust,Traits,对不起,我可能错过了一些明显的东西。我想知道为什么我不能这样称呼我的特质方法。这不应该是标准的UFC吗 trait FooPrinter { fn print () { println!("hello"); } } fn main () { FooPrinter::print(); } 游戏围栏: 我得到以下错误 error: type annotations required: cannot resolve `_ : FooPrinter` 如果
trait FooPrinter {
fn print () {
println!("hello");
}
}
fn main () {
FooPrinter::print();
}
游戏围栏:
我得到以下错误
error: type annotations required: cannot resolve `_ : FooPrinter`
如果不指定要在哪个实现上调用trait方法,则无法调用该方法。该方法是否具有默认实现并不重要 实际的UFCS调用如下所示:
trait FooPrinter {
fn print() {
println!("hello");
}
}
impl FooPrinter for () {}
fn main () {
<() as FooPrinter>::print();
}
打印机{
fn print(){
println!(“你好”);
}
}
(){}的impl foopprinter
fn main(){
::print();
}
如果此方法不需要多态性,请将其移动到
结构
或枚举
,或使其成为全局函数。有趣。因此,基本上,只有当trait方法采用某种类型的self
时,才可以使用UFCS
?哦,这当然是可能的。请参阅我的编辑。我没有详细说明,因为你的例子太少了,我以为你在工作中使用了错误的工具。哦,那更有趣!我只是随便看看语言特性;)