Rust generic type parameter and impl Trait parameter
(Jin Qing’s Column, Jun., 2023)
See: https://doc.rust-lang.org/reference/types/impl-trait.html
Generic type parameter and impl Trait
parameter are almost equivalent:
trait Trait {}
// generic type parameter
fn foo<T: Trait>(arg: T) {
}
// impl Trait parameter
fn foo(arg: impl Trait) {
}
impl Trait
is just a syntactic sugar for generic type parameter.
It is a little simpler by omitting the generic parameters <...>
.
But they are not exactly equivalent.
The caller can use a generic argument to specify the type of the generic parameter T,
for example, foo::<usize>(1)
, but not for the impl Trait
parameter.文章来源:https://www.toymoban.com/news/detail-492491.html
Therefore, changing the function signature from either one to the other
can constitute a breaking change.文章来源地址https://www.toymoban.com/news/detail-492491.html
到了这里,关于Rust generic type parameter and `impl Trait` parameter的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!