[Blog post] Variance in Rust: An intuitive explanation

#1

Hi

I’ve put together an intuitive explanation of Variance and why it’s important here: https://ehsanmkermani.com/2019/03/16/variance-in-rust-an-intuitive-explanation/

Any comments is more than welcome!

3 Likes

#2

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.

0 Likes

#3

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.

0 Likes

#4

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”.

0 Likes

#5

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.

0 Likes

#6

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.

2 Likes

#7

You may be interested in: https://medium.com/nearprotocol/understanding-rust-lifetimes-e813bcd405fa

0 Likes

#8

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.

0 Likes

#9

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

0 Likes