Step 1: Understand Copy trait effect on assignment
Since Color derives Copy, assigning c1 to c2 copies the data, not moves it.
Step 2: Check usage after assignment
Because c1 is still valid, printing its fields outputs the original values.
Final Answer:
255 0 0 -> Option A
Quick Check:
Copy means no move, data duplicated [OK]
Hint: Copy types allow use after assignment without move errors [OK]
Common Mistakes:
Expecting a move error after assignment
Thinking c1 is invalid after c2 = c1
Confusing Copy with Clone behavior
4. Identify the error in this Rust code snippet:
#[derive(Copy)]
struct Point { x: i32, y: i32 }
fn main() {
let p1 = Point { x: 5, y: 10 };
let p2 = p1;
println!("{} {}", p1.x, p1.y);
}
medium
A. Assignment moves ownership, so p1 is invalid
B. Cannot print struct fields directly
C. No error, code runs fine
D. Missing Clone trait derivation causes compile error
Solution
Step 1: Check trait derivations
The struct derives only Copy, but Copy requires Clone to be implemented too.
Step 2: Understand compiler requirements
Rust compiler will error because Clone is missing, even if Copy is derived.
Final Answer:
Missing Clone trait derivation causes compile error -> Option D
Quick Check:
Copy needs Clone too [OK]
Hint: Derive Clone along with Copy to avoid errors [OK]
Common Mistakes:
Assuming Copy alone is enough
Thinking printing fields causes error
Confusing move error with trait error
5. You want to create a struct Pixel with fields x: u32, y: u32, and color: (u8, u8, u8). You want Pixel to be a Copy type. Which of the following is the correct approach?
hard
A. Derive Copy and Clone for Pixel since all fields are Copy types
B. Implement Copy manually without Clone
C. Use String for color and derive Copy
D. Derive only Clone for Pixel
Solution
Step 1: Check field types for Copy trait
Fields u32 and tuple of u8 are all Copy types.
Step 2: Derive Copy and Clone for struct
Since all fields are Copy, deriving both Copy and Clone for Pixel makes it a Copy type.
Final Answer:
Derive Copy and Clone for Pixel since all fields are Copy types -> Option A
Quick Check:
All fields Copy + derive Copy & Clone = Copy type [OK]
Hint: All fields Copy? Derive Copy and Clone for struct [OK]
Common Mistakes:
Using String for color which is not Copy
Deriving only Clone without Copy
Trying to implement Copy manually
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.