AAA: what exactly happens when I *dereference something in Rust?
BBB: It depends on the context
When you write *v you either have a Box or a type that implements Copy
If it's a Box, you have to already have ownership
If it's a Copy value, it just copies it
When you write &**v, I think you are calling Deref::deref
And &mut**v should thus be DerefMut::deref_mut
CCC: unless it's on the left side of a =
BBB: Or rather on the left side of an assignment
CCC: technically still on the left side of a = :stuck_out_tongue:
But yeah, you are correct
BBB: Are my other cases also right?
I've been trying to find a source, but haven't been successful
CCC: &*v is the common way to explicitly invoke Deref::deref i think
BBB: Not if v is a &T, right?
CCC: If v is a String, &*v first calls String::deref which returns str, and then borrows that
BBB: True, but Deref::deref takes a shared reference
CCC: You rarely need to explicitly deref coerce though
BBB: I've needed it when implementing stuff on Box
CCC: Yeah, and you might need it when a function has a generic argument that is "too generic", so the compiler can't figure it out
@AAA originally (i.e. in C), dereferencing means following a pointer
It's not as simple in Rust though, due to its ownership rules and mutable references being unique
in C, dereferencing on the left side of an assignment, e.g. *v = x;, *v += x; etc, means to do the assignment to the value pointed to by v
and dereferencing anywhere else means read the value pointed to by v and give me a straight up copy of it