#Reborrowing question

5 messages · Page 1 of 1 (latest)

fickle belfry
#

the * operator is called dereferencing, it takes a reference (&Type) and converts it to a Type by copying the data behind the reference into a new variable.

You can't do it to a &Vec<T> because Vec<T> does not implement the Copy trait.

fickle belfry
#

Nothing, it still exists

fickle belfry
#

I believe its because you never assign *v to a variable. The first argument to Vec::push is a temporary variable so it doesnt need to move out of the *v

#

Oh no. That's wrong. I have no idea then. It does seem weird

#

Note that this does compile:

fn main(){
    let mut v = vec![1];
    foo(&mut v);
}

fn foo(v: &mut Vec<i32>) {
    let reborrow = &mut *v;
    Vec::push(reborrow, 0);
    Vec::push(v, 0);
}

But this doesnt compile

fn main(){
    let mut v = vec![1];
    foo(&mut v);
}

fn foo(v: &mut Vec<i32>) {
    let reborrow = &mut *v;
    Vec::push(reborrow, 0);
    Vec::push(v, 0);
    Vec::push(reborrow, 0);
}