Rust 需要为这个 UFCS 调用添加什么类型的注解?
What type annotations does Rust want for this UFCS call?
抱歉,我可能遗漏了一些 super 显而易见的东西。我想知道为什么我不能这样调用我的特征方法。这不应该是标准的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
,或将其设为全局函数。
抱歉,我可能遗漏了一些 super 显而易见的东西。我想知道为什么我不能这样调用我的特征方法。这不应该是标准的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
,或将其设为全局函数。