2
pub struct Triangle<T: Float + std::clone::Clone, V: vector::Vector<T>> {
    point1: V,
    point2: V,
    point3: V,
}

This chunck of code doesn't compile because T isn't used (Nevertheless, T is used later in a method)

I have tried this syntax

pub struct Triangle<V: vector::Vector<T: Float + std::clone::Clone>> {
    point1: V,
    point2: V,
    point3: V,
}

Error:

expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `:`

expected one of 7 possible tokens here

and this syntax

pub struct Triangle2<V> where V: vector::Vector<T> where T: Float {
    point1: V,
    point2: V,
    point3: V,
}

Error:

expected `where`, or `{` after struct name, found keyword `where`

expected `where`, or `{` after struct name

that doesn't work.

Is there a way to fix this problem?

pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
  • Could you also put the error message in the question? – so61pi Apr 04 '19 at 15:34
  • Is [this code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=95454216b6d196d20da4af17499c6961) what you are after? – so61pi Apr 04 '19 at 15:40
  • @so61pi Your solution (triangle {point : vector}) works here but triangle implements shape> and therefore it must have 2 generic parameters – Pierre-olivier Gendraud Apr 04 '19 at 15:45
  • Please **always** paste the ***complete*** error, not just a snippet of it. – hellow Apr 04 '19 at 16:28
  • Consider putting the bounds only on the method that needs them, as suggested by the answers to [Specify `Fn` trait bound on struct definition without fixing one of the `Fn` parameters](https://stackoverflow.com/questions/50671177/specify-fn-trait-bound-on-struct-definition-without-fixing-one-of-the-fn-par) – trent Apr 04 '19 at 17:25

1 Answers1

4

I assume your type Vector looks more or less like this.

pub trait Vector<T> {
    // Some functions
}

The solution is to declare multiple generic types and to list their type constraints individually: Type V must implement Vector<T> and type T in turn must implement Float and Clone.

pub struct Triangle<V, T>
where
    V: vector::Vector<T>,
    T: Float + Clone,
{
    point1: V,
    point2: V,
    point3: V,
    phantom: PhantomData<T>,
}

I'm using std::marker::PhantomData to save the otherwise unused type information.

Link to full working code.

Jan Hohenheim
  • 3,552
  • 2
  • 17
  • 42