1

I have 2 trait implementations in a file. How can I call the first_function from the second implementation of Trait?

impl<T: Trait> Module<T> {
    pub fn first_function() {
        // some code here
    }
}

impl<T: Trait> Second<T::SomeType> for Module<T> {
    pub fn second_function() {
        // Needs to call the first function available in first trait implementation.
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
not 0x12
  • 19,360
  • 22
  • 67
  • 133
  • Please review how to create a [MCVE] and then [edit] your question to include it. We cannot tell what types, traits, fields, etc. are present in the code. Try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) or in a brand new Cargo project. There are [Rust-specific MCVE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. – Shepmaster Feb 20 '19 at 16:10
  • *from the second implementation of `Trait`* — There's only **one** implementation of `Trait` here. – Shepmaster Feb 20 '19 at 16:13

1 Answers1

5

You need to use turbofish (::<>) syntax:

Module::<T>::first_function()

complete example:

struct Module<T> {
    i: T,
}

trait Trait {
    type SomeType;
}

trait Second<T> {
    fn second_function();
}

impl<T: Trait> Module<T> {
    fn first_function() {
        // some code here
    }
}

impl<T: Trait> Second<T::SomeType> for Module<T> {
    fn second_function() {
        Module::<T>::first_function();
    }
}

Playground

Also see this related question about the turbofish syntax:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
  • @WeslyWiser Please feel free to rollback my change if you feel that it goes against your actual answer. I just wanted to provide additional resources regatrdint the turbofish syntax – hellow Feb 20 '19 at 08:56