Rust 中逆变使用的例子是什么?

What is an example of contravariant use in Rust?

Nomicon's section about subtyping中,它表示逆变可用于函数指针类型。但是,我找不到任何好的例子。我试图编写一个带有函数指针的结构,但逆变似乎不起作用。

这方面的代码示例是什么?


the page you linked 上搜索词条 "contra" 有许多相关段落:

Actually witnessing contravariance is quite difficult in Rust, though it does in fact exist.

NOTE: the only source of contravariance in the language is the arguments to a function, which is why it really doesn't come up much in practice. Invoking contravariance involves higher-order programming with function pointers that take references with specific lifetimes (as opposed to the usual "any lifetime", which gets into higher rank lifetimes, which work independently of subtyping).

And that's why function types, unlike anything else in the language, are contravariant over their arguments.

页面以所有逆变类型的示例结尾。应用它...

逆变

struct MyContraType<Mixed> {
    k1: fn(Mixed), // contravariant over Mixed
}

fn contra_example<'short>(
    mut a: MyContraType<&'short u8>,
    mut b: MyContraType<&'static u8>,
    x: fn(&'short u8),
    y: fn(&'static u8),
) {
    a.k1 = x;
    a.k1 = y; // Fails
    b.k1 = x;
    b.k1 = y;
}

逆变例不允许用 'static 代替 'short:

error[E0308]: mismatched types
  --> src/lib.rs:12:12
   |
12 |     a.k1 = y;
   |            ^ lifetime mismatch
   |
   = note: expected type `fn(&'short u8)`
              found type `fn(&'static u8)`
note: the lifetime 'short as defined on the function body at 5:19...
  --> src/lib.rs:5:19
   |
5  | fn contra_example<'short>(
   |                   ^^^^^^
   = note: ...does not necessarily outlive the static lifetime

协方差

struct MyCoType<Mixed> {
    k1: fn() -> Mixed, // covariant over Mixed
}

fn co_example<'short>(
    mut a: MyCoType<&'short u8>,
    mut b: MyCoType<&'static u8>,
    x: fn() -> &'short u8,
    y: fn() -> &'static u8,
) {
    a.k1 = x;
    a.k1 = y;
    b.k1 = x; // Fails
    b.k1 = y;
}

协变示例不允许用 'short 代替 'static:

error[E0308]: mismatched types
  --> src/lib.rs:29:12
   |
29 |     b.k1 = x;
   |            ^ lifetime mismatch
   |
   = note: expected type `fn() -> &'static u8`
              found type `fn() -> &'short u8`
note: the lifetime 'short as defined on the function body at 21:15...
  --> src/lib.rs:21:15
   |
21 | fn co_example<'short>(
   |               ^^^^^^
   = note: ...does not necessarily outlive the static lifetime