The manifestation of Rc <Vec <T>> confusion in rust
Why does the following code work?
use std::rc::Rc;
fn main () {
let c = vec![1, 2, 3, 4, 5];
let r = Rc::new(c);
println!("{:?}", (**r)[0]);
}
I can understand that it works with only respect ( println!("{:?}", (*r)[0]);). But not able to understand that it works with double dereferencing.
+4
2 answers
It may be easier to understand what happens when we create a function prototype around an expression (**r)[0]:
fn foo<T, U>(r: T) -> i32
where
T: Deref<Target=U>,
U: Deref<Target=[i32]>,
{
(**r)[0]
}
Rc<T>, - Rust, Deref, . , Vec<T> Deref, (Target = [T]). *, , .
, , Vec Index.
+6