Option is invariant with respect to its argument type?

When I was reading rfcs-0738-variance, I was confused about "Option is invariant with respect to its argument type". I think it should be covariant. Moreover, there are no compilation errors in the codes in the rfcs, but the rfcs states that there should be "ERROR: Cannot infer an appropriate lifetime".
I'm confused. Thanks for the help.

struct List<'l> {
    field1: &'l i32,
    field2: Option<&'l i32>,

fn foo<'a>(field1: &i32, field2: Option<&i32>) {
    let list = List { field1: field1, field2: field2 };
    // ERROR: Cannot infer an appropriate lifetime
    println!("{:?}", list);

fn main() {

That was true when the RFC was written (2014, pre-1.0). The RFC was arguing that it should be covariant -- and due to that RFC being implemented[1], now it is.

  1. in some form, I didn't look to see how far things strayed from the RFC ↩︎

1 Like


Why/how was a simple by-value wrapper invariant?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.