【发布时间】:2019-01-28 11:27:16
【问题描述】:
我正在尝试将C 类型参数添加到此代码(playground):
use std::ops::Index;
struct ConnectionHandle(usize);
struct Connection<C>(C);
impl<C> Index<ConnectionHandle> for Vec<Connection<C>> {
type Output = Connection<C>;
fn index(&self, ch: ConnectionHandle) -> &Self::Output {
&self[ch.0]
}
}
但这样做会导致此错误消息:
error[E0210]: type parameter `C` must be used as the type parameter for some local type (e.g. `MyStruct<C>`)
--> src/lib.rs:6:1
|
6 | impl<C> Index<ConnectionHandle> for Vec<Connection<C>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type parameter `C` must be used as the type parameter for some local type
|
= note: only traits defined in the current crate can be implemented for a type parameter
为什么不允许这样做? Connection 是本地的,所以根据 E0201 的解释,这似乎不应该导致孤儿。
【问题讨论】: