rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.75k stars 256 forks source link

Suggestion to rewrite variance in fuction arguments explanation #394

Open andronov-alexey opened 1 year ago

andronov-alexey commented 1 year ago

I have a hard time trying to understand variance in function arguments in the following chapter:

However, the same logic does not apply to 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. Covariance doesn't work here. But if we flip it around, it actually does work! If we need a function that can handle Cats, 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.

The most confusing thing is what does it mean to "satisfy one function with the other"?

I'd suggest to rewrite this paragraph

sgasse commented 1 year ago

I read the example today as well and through about fleshing it out a little bit but I do not want to get into anyone's way. @andronov-alexey do you want to work on this and just wait for a thumbs-up from a maintainer, or did you just want to bring it up?

andronov-alexey commented 1 year ago

@sgasse I just wanted to bring it up and I'd really appreciate if someone rewrote it :)