抱歉,我可能遗漏了一些非常明显的东西。我想知道为什么我不能这样调用我的特征方法。这不应该是标准的 UFCS 吗?
trait FooPrinter {
fn print () {
println!("hello");
}
}
fn main () {
FooPrinter::print();
}
出现以下错误
error: type annotations required: cannot resolve `_ : FooPrinter`
最佳答案
如果不指定要调用哪个实现,就不能调用特征方法。该方法是否具有默认实现并不重要。
实际的 UFCS 调用如下所示:
trait FooPrinter {
fn print() {
println!("hello");
}
}
impl FooPrinter for () {}
fn main () {
<() as FooPrinter>::print();
}
如果您不需要此方法的多态性,请将其移至struct
或enum
,或将其设为全局函数。
关于rust - Rust 需要为这个 UFCS 调用添加什么类型的注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374149/