Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are reference values copied in Rust? [duplicate]

Tags:

reference

rust

Am I correct to assume that, for the following code

let a = vec![1, 2, 3];
let b = &a;
let c = b;

The memory presentation will be something like this, assuming the value of b is "B"?

  _            _
b|B|         c|B|
  |____________|
  |
  V
  _________
a|_________|

I'm only asking about immutable references, as there can be only 1 mutable reference, as far as I remember.

like image 270
Exander Avatar asked Mar 30 '17 18:03

Exander


People also ask

What is copy reference?

n. A copy of a record kept for easy access to the information it contains, as opposed to its intrinsic or evidential value. A copy of a record distributed to make recipients aware of the content but not directing the recipient to take any action on the matter.

Which does not implement copy trait?

A String is a type that does not implement the Copy trait.

How do you copy variables in rust?

You can just use . clone() for your duplicate function.


1 Answers

Yes, this is correct.

In Rust terms, &T is Copy, which means that it can be copied bitwise without transferring ownership.

like image 98
Matthieu M. Avatar answered Sep 28 '22 00:09

Matthieu M.