Happy new year ,
It's obvious that dropping arr
as below will cause undefined behavior,
since the uninitialized arr
will be considered as a valid String
array and destructor of String
will be invoked on each uninitialized element of arr
.
let arr: [String; 4] = unsafe {
std::mem::MaybeUninit::uninit().assume_init()
};
std::mem::drop(arr);
My main question is:
Would dropping an uninitialized array of T
where T: Copy
lead to undefined behavior?
let arr: [u64; 4] = unsafe {
std::mem::MaybeUninit::uninit().assume_init()
};
std::mem::drop(arr);
T: Copy
cannot implement Drop
. Thus when a [T; 4]
is dropped,
I expect that no access will be made on T
,
and the stack memory occupied by the array will simply be retrieved.
My feelings tell me that dropping an uninitialized array of T: Copy
wouldn't lead to undefined behavior, but I'm not sure...