Stream: t-compiler/help

Topic: How to understand &**&Vec[T]?


Yan Luo (Aug 09 2019 at 03:36, on Zulip):

Hello, I am reading the Clone implementation for Vec[T] in vec.rs. In the implementation of the Clone trait for Vec[T], the "clone" fn is like:
fn clone(&self) -> Vec[T] {
<[T]>::to_vec(&**self)
}
In my understanding, self here is a reference. *self is still a reference to Vec[T], and **self is also a reference to Vec[T] because Deref for &T returns &T. Why are there 2 deref?

Thanks!

Nick12 (Aug 23 2019 at 02:03, on Zulip):

**self just dereferences self twice, once to go from &Vec to Vec, and once more, I assume to use the Deref impl to go to a [T]. Then it takes a ref, making it a slice, and calls slice::to_vec on it

Last update: Nov 11 2019 at 21:55UTC