E4017#
方法的类型有歧义,可能来自多个特征。
错误示例:#
struct S { v : Int } derive(Show)
trait Tee { to_string(Self) -> String }
impl Tee for S with to_string(_self) { "Tee" }
let s : S = { v: 3 }
let t = s.to_string()
上述例子中,试图在类型 S
上调用方法 to_string
,但方法名称来自 Show
和 Tee
两个特征,会在第 8 行报错:
Method to_string of type S is ambiguous, it may come from trait Tee or Show
建议#
通过指明具体的特征对方法去歧义:
struct S { v : Int } derive(Show)
trait Tee { to_string(Self) -> String }
impl Tee for S with to_string(_self) { "Tee" }
let s : S = { v: 3 }
let t = Tee::to_string(s)