Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust希望此UFCS调用使用什么类型的注释?_Rust_Traits - Fatal编程技术网

Rust希望此UFCS调用使用什么类型的注释?

Rust希望此UFCS调用使用什么类型的注释?,rust,traits,Rust,Traits,对不起,我可能错过了一些明显的东西。我想知道为什么我不能这样称呼我的特质方法。这不应该是标准的UFC吗 trait FooPrinter { fn print () { println!("hello"); } } fn main () { FooPrinter::print(); } 游戏围栏: 我得到以下错误 error: type annotations required: cannot resolve `_ : FooPrinter` 如果

对不起,我可能错过了一些明显的东西。我想知道为什么我不能这样称呼我的特质方法。这不应该是标准的UFC吗

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
?哦,这当然是可能的。请参阅我的编辑。我没有详细说明,因为你的例子太少了,我以为你在工作中使用了错误的工具。哦,那更有趣!我只是随便看看语言特性;)