[Blog post] Variance in Rust: An intuitive explanation


I've put together an intuitive explanation of Variance and why it's important here: Variance in Rust: An intuitive explanation - Ehsan's Blog

Any comments is more than welcome!


Would it be possible to explain it without using the term type constructor?

I don't think ever of constructing types, and when you say constructor I think of fn new(), so I'm already lost in the second paragraph.

Just FWIW (not necessarily disagreeing with your feedback), you have the right idea with respect to fn new(), but instead of thinking about it at the value level, it just applies to the type level. e.g., If you want to create a value Foo but all you have is a Foo::new, then you have a constructor for Foo rather than a Foo itself.

The same applies to types. For example, Vec is not actually a concrete type in the exact same way that Foo::new is not actually a Foo. A Vec is just something you can use to build a concrete type by applying it to another concrete type.

Starting a conversation about variance by limiting its scope to type constructors helps orient yourself. But you're right of course. It only helps orient oneself insomuch as you already know what "type constructor" means. I think maybe keeping the term by adding some examples to the blog post would help.

I would use the expression "generic type" since I think it's more easily understandable for your target audience, and maybe add a footnote where you talk about type constructor being "the more accurate name".

I sort of get it, but type constructor is not part of usual Rust vocabulary. When I program in Rust I don't think of "constructing" types, even though I am technically constructing types. To me it's just an alien term. Even if that's the most technically accurate term, it doesn't ground the explanation in my existing intuition.

Great! thank you all for you feedbacks and suggestions @kornel @BurntSushi @Yandros. This is exactly what I was looking for.

I've just updated the blog post and removed any mention of type constructor but only at the very end. Hope it's more accessible now.


You may be interested in: Understanding Rust Lifetimes. No, seriously, this time for real | by Maksym Zavershynskyi | NEAR Protocol | Medium

Thanks! Pretty good. I've already referred to it in my presentation. I wanted to explain it without any particular formalism and get to the point fast.

It's a nice article, but do note that the author gets the lifetime bounds wrong at the end of the article.