Is Deref for accessing underlying collection idiomatic Rust?


#1

Hi all,

I was wondering whether it was considered as idiomatic Rust to implement Deref for accessing a collection in this case:

struct S<T> {
    v: Vec<T>,
}

Then all iterators are accessible through Deref if implemented (like iterating over S is iterating over v)?


#2

You’re probably looking for AsRef<[T]>; Deref is more for smart pointer semantics.


#3

Thanks @vitalyd, I’ll have a look at AsRef.