I've got some code dealing with hashmaps of vectors, and I encountered a problem that I don't quite understand. Since I have a HashMap<Vec<_>>, calling get() returns an Option<&Vec<_>>. I would like to turn this into a Option<&[_]>, and Option::as_deref() seems like it would do exactly that. However, calling as_deref() seems to have no effect at all and just gives me back the original type.
Some example code showing the same problem:
let foo: Option<Vec<u32>> = Some(vec![1, 2, 3]);
let bar: Option<&Vec<u32>> = foo.as_ref();
let baz: Option<&[u32]> = bar.as_deref(); // Does not compile: expected slice `[u32]`, found struct `Vec`
To further explain the purpose of as_deref: it's meant to be called on &Option<Vec<T>>, not Option<&Vec<T>>, i. e. you could skip the first as_ref step, as it's designed to be an alternative to that. In other words, it's a shorthand for the whole of .as_ref().map(ops::Deref::deref).
I do agree that it not doing anything on Option<&T> can seem confusing, and the documentation could probably be improved to point that out explicitly, and name the alternative – IMO it could also call out the longer alternative that it's a shorthand for that I mentioned above.