如何在泛型类型上调用关联函数?

时间:2019-02-20 01:52:50

标签: rust traits

我在文件中有2个trait实现。我该如何从first_function的第二个实现中调用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.
    }
}

1 个答案:

答案 0 :(得分:1)

您需要使用turbofish(const float borderWidth = 8.0f; Pen borderPen = new Pen(Color.Red, borderWidth) { Alignment = System.Drawing.Drawing2D.PenAlignment.Inset }; public Form2() { InitializeComponent(); this.Paint += Form2_Paint; this.Resize += Form2_Resize; } private void Form2_Resize(object sender, EventArgs e) { Invalidate(); } private void Form2_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(borderPen, this.ClientRectangle); } )语法:

::<>

完整示例:

Module::<T>::first_function()

Playground

另请参阅有关turbofish语法的以下相关问题: