3 ms·
In Rust, types implement the Copy trait and the Drop (destructor) trait independently. Vec could easily implement Copy. It doesn't because copying a Vec requi
by mleonhard 4y ago
In Rust, types implement the Copy trait and the Drop (destructor) trait independently. Vec could easily implement Copy. It doesn't because copying a Vec requires an expensive heap allocation, which can surprise users when it happens implicitly.
- est31 4y agoNo you can't, there is a specific error for that. You can try it: struct Foo; impl Copy for Foo {} impl Drop for Foo { fn drop(&mut self) { // ... } } It gives the error: error[E0184]: the trait `Copy` may not be implemented for this type; the type has a destructor --> src/main.rs:3:1 | 3 | impl Copy for Foo {} | ^^^^^^^^^^^^^^^^^^^^ `Copy` not allowed on types with destructors See also the "When can't my type be Copy?" section of the Rust documentation: https://doc.rust-lang.org/1.62.0/std/marker/trait.Copy.html#when-cant-my-type-be-copy https://doc.rust-lang.org/1.62.0/std/marker/trait.Copy.html#...