Rust Systems & Tokio (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What is the difference between `Copy` and `Clone` traits?
junior LevelrustBasic
In Simple Words:
CopyTrait: Implicit, low-cost bitwise memcpy on the Stack:- Implemented for fixed-size scalar types (
i32,bool, tuples of Copy types). let y = x;copies the bits;xremains completely valid.
- Implemented for fixed-size scalar types (
CloneTrait: Explicit deep copy that may duplicate Heap memory:- Requires calling
.clone()explicitly (let s2 = s1.clone();). - Signals to the developer that an expensive heap memory allocation is occurring.
- Requires calling