Quick response; C ++ types POD β Rust Copy types .
However, there are several other related concepts. Let's discuss them in detail!
The Copy sign is closest to the term "POD type" in C ++ and is defined as follows:
Types whose values ββcan be duplicated simply by copying bits.
Under "bit copying" the documentation basically means memcpy() . This includes all primitive types, such as u32 , f32 , char and bool , but user-defined types can also be Copy . Usually a sign is simply displayed:
#[derive(Clone, Copy)] struct Point { x: f32, y: f32, }
You may have noticed Clone : Clone trait is a Copy requirement and is defined as:
A common feature for the ability to explicitly duplicate an object.
Clone says that the type is "somehow capable of duplicating itself", and Copy requires more, saying that the type is "capable of duplicating itself, simply by copying a bit of the type."
The C ++ answer states that POD types do not contain "constructors, destructors, and virtual member functions." Let me break this down for Rust:
Constructors : Rust does not have dedicated constructor methods, but instead uses related functions (static methods in C ++). Each type, even all types of Copy , can have as many related functions and methods as they need. Rust does not really have to be a "POD". In particular, even primitive types of Rust have many methods, for example u32::pow() . This restriction does not apply to Rust.
Destructors : in Rust, objects are destroyed by calling drop() from Drop (or rather: drop() automatically called at the end of the scope). Types cannot be Copy when they implement the Drop trait! . Rust has a similar limitation here.
Virtual member functions : in Rust, virtual is not a property of a function. Most functions can be used in a virtual context, that is: they can be used with dynamic dispatch, but Copy does not prevent the use of a type in the context of dynamic distribution (in terms of Rust: used as an object-object). This is partly due to the fact that vptr is not stored in the type, but next to the pointer to the object (fat pointers). This item does not apply to Rust.
source share