generics - Trait::<T> 和 <Trait<T>> 有什么区别?

标签 generics rust

我有一个类型,我可以通过它访问它的方法

SomeTrait::<T>::method()

但我不明白那和之间的区别
<SomeTrait<T>>::method()

在 C++ 中,我希望这样:
SomeTrait<T>::method()

这两个有区别吗?他们似乎都在调用<T> method 的特化在 SomeTrait .

最佳答案

不能使用 C++ 语法,因为它在 Rust 中是一种模棱两可的语法:在 SomeTrait<T>::method() 中, 是第一个 <小于运算符,还是泛型参数列表的开头?

您提到的两种方法用于消除歧义:

  • <SomeTrait<T>>被称为 fully qualified syntax
  • SomeTrait::<T>被称为turbofish符号(非官方名称)。
  • 关于generics - Trait::<T> 和 <Trait<T>> 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59140724/

    相关文章:

    c# - 这是使用泛型方法的好方法吗?

    c# - 如何将 repository<T> 中的泛型 T 转换为接口(interface)以在 LINQ to SQL 过滤器中有条件地访问接口(interface)属性?

    _Generic 可以是一个函数吗?或者 _Generic 可以与 __attribute__((cleanup)) 一起使用吗?

    rust - 共享生命周期的力场

    rust - 在线程内使用闭包

    rust - 是否可以使用宏生成结构?

    unit-testing - Cargo 未在顶级文件中运行测试

    java - 在 java 中使用泛型时设置 intellij 询问类型参数

    c# - ILookup<TKey, TElement> 不应该在 TElement 中(声明)协变吗?

    rust - 写入 BufWriter 的字节实际上并未写入