Why does Vec<T, A> implement AsMut<Vec<T, A>>?

If I understand correctly, AsMut is for conversion between references, for example AsMut<str> for String converts a mutable reference of String to a mutable reference of str. It's pretty logical.

But what does that mean? (Vec trait implementations)

impl<T, A> AsMut<Vec<T, A>> for Vec<T, A>

It just converts a mutable reference of Vec<T, A> to a mutable reference of the same type. What's the point? Maybe it just creates a brand new references of the same type?

These sort of tautological impls are useful for making generic code work. It's the same reason why impl From<T> for T exists.

6 Likes

Because it lets you write functions that take impl AsMut<Vec<u32>>, and still pass Vec<u32> to it.

3 Likes

See also std::convert::AsRef#Reflexivity.

2 Likes

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.