You have to differentiate based on the underlying type.
You can't do a shallow copy of struct Foo(String); because two instances of Foo would point at the very same String, which would violate the strict aliasing rule .
However, if you are having a reference counter, e.g. struct Foo(Rc<String>); it is possible, because Rc will prevent you from doing unsafe things, e.g. having a mutable reference, when there are others references on the String.
It is also possible for types that implement [Clone] to make a "shallow" copy, because Copy implies, that the type can be copied by using memcpy (e.g. a u32 or a &T).
So, in general no: Exceptions are reference counted structs (Rc or Arc) , Cloneables, or references (&T), because they don't violate the strict aliasing rule.
You have to differentiate based on the underlying type.
You can't do a shallow copy of
struct Foo(String);
because two instances ofFoo
would point at the very sameString
, which would violate the strict aliasing rule .However, if you are having a reference counter, e.g.
struct Foo(Rc<String>);
it is possible, becauseRc
will prevent you from doing unsafe things, e.g. having a mutable reference, when there are others references on theString
.It is also possible for types that implement [
Clone
] to make a "shallow" copy, becauseCopy
implies, that the type can be copied by usingmemcpy
(e.g. au32
or a&T
).What is the difference between Copy and Clone? is also very worth reading.
So, in general no: Exceptions are reference counted structs (
Rc
orArc
) ,Cloneables
, or references (&T
), because they don't violate the strict aliasing rule.