generics - `my_func<T: MyTrait>` 与 `my_func<T> where T: MyTrait` 之间的区别

标签 generics rust traits

<分区>

有什么区别

my_func<T: MyTrait>(...) {...}

my_func<T>(...) where T: MyTrait {...}

?

最佳答案

一个是用明确的 where 子句编写的,另一个不是。

好的,好的;如果你想挑剔的话,区别是:

  • 您不能使用 where 子句引入通用参数。
  • 除了通用参数列表中的通用参数外,您不能限制任何

但是就将约束从通用参数列表移动到 where 子句时发生的变化而言,它们是相同的。

关于generics - `my_func<T: MyTrait>` 与 `my_func<T> where T: MyTrait` 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465943/

相关文章:

c - Rust如何将结构转移回C?

unit-testing - rust 中的单元测试、模拟和特征

c# - 使用字典中的抽象泛型类作为值而不具体说明类型

rust - 为什么特征类型 `Box<dyn Error>` 错误与 "Sized is not implemented"但 `async fn() -> Result<(), Box<dyn Error>>` 有效?

rust - 在可变地迭代数组的同时访问数组

python - 特征列表不报告添加或删除的项目

rust - supertrait 的默认实现

基于对象类型的 Java getter 返回类型

c# - "You cannot save a class that does not contain a property that represents the item ID"当 GlassCast(ing) 分支模板项时,为什么?

.net - 如何从 wcf 服务中检索未知类型?