我想知道如何从一个特征调用一个函数,前提是有多个特征具有相同的函数名称。
问题出在第33行或者
tr1::tr(v);
我如何表达我想要调用哪个特质?
struct V2D {
x: i32,
y: i32
}
impl V2D {
fn new(x: i32, y: i32) -> V2D {
V2D { x, y }
}
}
trait tr1 {
fn tr();
}
trait tr2 {
fn tr();
}
impl tr1 for V2D {
fn tr() {
println!("This is tr1");
}
}
impl tr2 for V2D {
fn tr() {
println!("This is tr2");
}
}
fn main() {
let v = V2D::new(1,2);
tr1::tr(v);
}
最佳答案
如果您的方法具有 self
参数 ( Permalink to the playground ),则您使用的语法 (tr1::tr(v)
) 是正确的,但如果事实并非如此,您需要在明确指定类型和特征的类型上调用它:
<V2D as tr1>::tr()
关于rust - 如何显式地从某些特征调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56612060/