pub struct Triangle<T: Float + std::clone::Clone, V: vector::Vector<T>> {
point1: V,
point2: V,
point3: V,
}
这段代码无法编译,因为未使用IT(尽管如此,T稍后会在方法中使用)
我试过这个语法
pub struct Triangle<V: vector::Vector<T: Float + std::clone::Clone>> {
point1: V,
point2: V,
point3: V,
}
错误:
expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `:`
expected one of 7 possible tokens here
和这个语法
pub struct Triangle2<V> where V: vector::Vector<T> where T: Float {
point1: V,
point2: V,
point3: V,
}
错误:
expected `where`, or `{` after struct name, found keyword `where`
expected `where`, or `{` after struct name
这不起作用。
他们是解决这个问题的方法吗?
我假设您的类型Vector
看起来或多或少像这样。
pub trait Vector<T> {
// Some functions
}
解决方案是声明多个泛型类型并单独列出它们的类型约束:类型V
必须实现Vector<T>
并且类型T
依次必须实现Float
和Clone
。
pub struct Triangle<V, T>
where
V: vector::Vector<T>,
T: Float + Clone,
{
point1: V,
point2: V,
point3: V,
phantom: PhantomData<T>,
}
我正在使用std::marker::PhantomData来保存其他未使用的类型信息。