简体   繁体   English

Rust function 指针逆变

[英]Rust function pointer Contravariant

I having trouble getting my head around contravariance in Rust.我很难理解 Rust 中的逆变性。

Specifically:具体来说:

However, the same logic does not apply to arguments.然而,同样的逻辑不适用于 arguments。 Consider trying to satisfy:考虑尝试满足:

fn handle_animal(Animal);

with

fn handle_animal(Cat);

The first function can accept Dogs, but the second function absolutely can't.第一个 function 可以接受 Dogs,但第二个 function 绝对不能。 Covariance doesn't work here.协方差在这里不起作用。 But if we flip it around, it actually does work, If we need a function that can handle Cats.但是如果我们翻转它,它确实可以工作,如果我们需要一个可以处理 Cats 的 function。 a function that can handle any Animal will surely work fine: Or to relate it back to real Rust: if we need a function that can handle anything that lives for at least 'long , it's perfectly fine for it to be able to handle anything that lives for at least 'short .可以处理任何 Animal 的 function 肯定可以正常工作:或者将其与真正的 Rust 联系起来:如果我们需要一个 function 可以处理任何生命至少'long的东西,它能够处理任何至少活得'short

(from: https://doc.rust-lang.org/nomicon/subtyping.html ) (来自: https://doc.rust-lang.org/nomicon/subtyping.html

It's this part I can't get my head around:这是我无法理解的部分:

Or to relate it back to real Rust: if we need a function that can handle anything that lives for at least 'long , it's perfectly fine for it to be able to handle anything that lives for at least 'short .或者将它与真正的 Rust 联系起来:如果我们需要一个 function 可以处理任何至少可以存活'long的东西,那么它能够处理至少可以存活'short的任何东西是完全可以的。

Wouldn't that not work as if you pass 'short somewhere where it needs 'long, it would mean that the 'short being passed in would not live long enough?如果你在需要'long'的地方传递'short',这不会不起作用吗,这意味着传递的'short'不会活得足够长?

I am aware that when it comes to lifetimes if 'long: 'short , then 'long is a subtype of 'short (as 'long is 'short and more), even with this in mind, I'm still struggling with the above..我知道,当谈到生命周期时,如果'long: 'short ,那么'long是 'short 的子类型(因为'long'short等等),即使考虑到这一点,我仍然在为上述问题苦苦挣扎..

Can someone help me make sense of it?有人可以帮我理解吗?

Also, when it come to variance, why do we say 'over' as in: &mut T is invariant over T' ?另外,当涉及到方差时,为什么我们说“over”如下: &mut T is invariant over T'

Wouldn't that not work as if you pass 'short somewhere where it needs 'long , it would mean that the 'short being passed in would not live long enough?如果您在需要'long的地方传递'short ,这不是不行吗,这意味着传递的'short不会活得足够长?

It's the inverse.这是相反的。 We are passing in a 'long where we need a 'short , and that works.我们正在传递一个'long我们需要一个'short的地方,这很有效。

Break down the quote:分解报价:

if we need a function that can handle anything that lives for at least 'long如果我们需要一个 function 可以处理任何可以存活至少'long

We need a function that can accept 'long , which means we must have 'long (or even longer) we are going to pass to it.我们需要一个可以接受'long的 function ,这意味着我们必须有'long (甚至更长)才能传递给它。

it's perfectly fine for it to be able to handle anything that lives for at least 'short能够处理至少“短暂”的任何事物,这完全没问题

So, we have a 'long to pass as an argument, and a provided function that accepts 'short .因此,我们有一个'long作为参数传递,以及一个提供的 function 接受'short That's OK.没关系。 The function needs it to live at least 'short and it does since it lives at least 'long . function 需要它至少能活得'short ,因为它至少能活'long

Answering the other question:回答另一个问题:

Also, when it come to variance, why do we say 'over' as in: &mut T is invariant over T'?另外,当谈到方差时,为什么我们说“over”,如:&mut T is invariant over T'?

If there are multiple parameters, then there may be different variances for the different parameters.如果有多个参数,那么不同的参数可能会有不同的方差。

For example &'a mut T has two parameters, 'a and T .例如&'a mut T有两个参数, 'aT It is covariant over 'a and invariant over T .它在'a上是协变的,在T上是不变的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM