我发现自己在同一边界下编写带有参数的不同函数,如下所示:
pub fn foo<T>(mut self, path: T) -> Self where
T: IntoIterator,
T::Item: AsRef<str>,
{
// ...
}
pub fn bar<T>(mut self, path: T) -> Self where
T: IntoIterator,
T::Item: AsRef<str>,
{
// ...
}
感觉这有点繁琐,我试着给这些界限起别名。但是我没有找到办法。在检查了几个地方[1],[2] 之后,我得到的最接近的是:
trait Path {
type I: IntoIterator<Item = Self::S>;
type S: AsRef<str>;
}
impl<T, U> Path for T where
T: IntoIterator<Item = U>,
U: AsRef<str>,
{
type I = T;
type S = U;
}
现在,举个例子,编译得很好:
fn into_vec<T: Path>(it: T::I) -> Vec<String> {
it.into_iter()
.map::<String, _>(|x| x.as_ref().into())
.collect()
}
但是当我尝试使用它时:
fn consume<T: Path>() {
into_vec::<T>(&["one", "two"]);
}
我收到以下错误:
src/lib.rs:104:19: 104:34 error: mismatched types:
expected `<T as Path>::I`,
found `&[&str; 2]`
(expected associated type,
found &-ptr) [E0308]
src/lib.rs:104 into_vec::<T>(&["one", "two"]);
^~~~~~~~~~~~~~~
所以,运气不好。我该如何前进?
1 https://github.com/rust-lang/rust/issues/8634
2 https://stackoverflow.com/a/30424219/3957040
最佳答案
How can I go forward?
你不能直接。让我们看看您的功能:
fn consume<T: Path>() {
into_vec::<T>(&["one", "two"]);
}
这表示“对于实现Path
的任何 类型,使用一段字符串调用into_vec
”。但是,您无法保证无论 T
是什么,它都会接受一段字符串。
从另一个方向看,许多类型可能接受一段字符串,因此这会使 T
变得不明确。
从第三方来看,类型推断无法确定 T
应该是什么,因为它不用作参数或返回值。
你可以通过明确说明你想要哪个 T
来让它工作:
fn consume() {
into_vec::<&[&'static str]>(&["one", "two"]);
}
澄清一下,这与将特征组合成另一个特征无关。这将是任何特征的问题。
关于generics - 在不指定具体关联类型的情况下别名特征边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35327525/