#Choice of assignment option question

6 messages · Page 1 of 1 (latest)

worthy locust
#

"If I just assign str1 to str2 outright then str1 disappears when I try to print it and I get an error that the compiler freed it."
It actually "disappears" during assignment but that doesn't cause any problems until you try to use it by printing it.

ornate matrix
#

Ownership manages freeing

#

Giving ownership of str1 to str2 doesn't free str1, it makes str2 the owner. Then, when str2 goes out of scope, it's freed.

#

Something is freed when there is no more owner

#

Other than that, sounds accurate

worthy locust
#

The difference between using a reference and cloning goes beyond what can be freed. Making a clone uses more memory and takes more time, because a full replica of the data is made. A reference doesn't make a replica. It just refers to the original data. It is possible to make a mutable clone and modify it without the original being changed, because it's a completely independent duplicate of the data.