问题描述
我正在阅读 RFC当我遇到以下情况时,就可以扩展"隐式特质:
虽然我从逻辑的角度理解普遍性与存在性,但是什么使第一个高于普遍性而第二个存在性呢?
While I understand universal vs existential from a logic perspective, what makes the first one above universal and the second one existential?
推荐答案
RFC以多种方式多次定义了这些术语:
The RFC defines the terms multiple times in multiple manners:
现有量化,即对于某些类型T",即被呼叫者" 选择".这就是今天impl Trait
的工作方式(作为回报 仅位置).当您写fn foo() -> impl Iterator
时,您就是 说函数将产生某种实现的T
类型 Iterator
,但是不允许调用者承担其他任何事情 关于该类型.
Existential quantification, i.e. "for some type T", i.e. "callee chooses". This is how impl Trait
works today (which is in return position only). When you write fn foo() -> impl Iterator
, you're saying that the function will produce some type T
that implements Iterator
, but the caller is not allowed to assume anything else about that type.
TL; DR:
-
fn take_iter(t: impl Iterator)
-呼叫take_iter
的人选择具体类型.该功能必须对实现特征的类型的整个宇宙"起作用.
fn take_iter(t: impl Iterator)
— the person callingtake_iter
picks the concrete type. The function has to work for the entire "universe" of types that implement the trait.
fn give_iter() -> impl Iterator
-give_iter
的实现选择具体类型.有一些类型存在"并实现该函数将返回的特征.
fn give_iter() -> impl Iterator
— the implementation of give_iter
picks the concrete type. There is some type which "exists" and implements the trait that will be returned by the function.
这篇关于是什么使"impl特性"作为参数“通用"?并且作为返回值"existential"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!