What is a concise way to inform the compiler of the specifc type with multiple implementations of a generic trait?

我只是一个虾纸丫 提交于 2019-11-29 16:58:46
red75prime

Pass the type parameter to the trait using a turbofish (::<>):

assert_eq!("asd", Foo::<&str>::foo(&bar));

You could also use fully-qualified syntax to disambiguate which trait the method belongs to:

// This is "type-qualified" and equivalent to `Foo::<&str>::foo(&bar)`
assert_eq!("asd", <_ as Foo<&str>>::foo(&bar));

// This is "fully qualified"
assert_eq!("asd", <Bar as Foo<&str>>::foo(&bar));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!