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
source share
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]
}

Playground

Rc<T>, - Rust, Deref, . , Vec<T> Deref, (Target = [T]). *, , .

, , Vec Index.

+6

, Rc Vec Deref, whichs deref - *.

let c = vec![1, 2, 3, 4, 5];

Vec vec! -macro.

let r = Rc::new(c);

. Rc.

println!("{:?}", (**r)[0]);

: *r > Rc, . *rc dereferences slice. slice[0] , 1. println! .

+7

Source: https://habr.com/ru/post/1694230/


All Articles