Bird
Raised Fist0
Rustprogramming~10 mins

Copy types in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Copy types
Declare variable x
↓
Assign x to y
↓
Copy happens: y gets a copy of x
↓
Use x and y independently
↓
End
This flow shows how a variable with a Copy type is duplicated when assigned to another variable, allowing both to be used independently.
Execution Sample
Rust
let x = 5;
let y = x;
println!("x = {}, y = {}", x, y);
This code copies the value of x into y because i32 is a Copy type, so both can be used after.
Execution Table
StepActionVariable xVariable yNotes
1Declare x = 55undefinedx is initialized with 5
2Assign y = x55Copy of x's value assigned to y
3Print x and y55Both variables hold 5 independently
4End55Program ends, no ownership moved
💡 Program ends after printing both x and y, both valid because i32 is Copy type
Variable Tracker
VariableStartAfter 1After 2After 3Final
xundefined5555
yundefinedundefined555
Key Moments - 2 Insights
Why can we still use x after assigning it to y?
Because i32 is a Copy type, assigning x to y copies the value instead of moving ownership, so x remains valid (see execution_table step 2 and 3).
What happens if the type is not Copy?
If the type is not Copy, assigning it to another variable moves ownership, making the original variable invalid (not shown here, but different from this example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of y after step 2?
A5
Bundefined
C0
Derror
💡 Hint
Check the 'Variable y' column at step 2 in the execution_table.
At which step does y get its value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column and when y changes from undefined to 5.
If x was a non-Copy type, what would happen after assigning y = x?
ABoth x and y would have the value
By would be undefined
Cx would be invalid, y would have the value
DProgram would crash immediately
💡 Hint
Recall Rust ownership rules for non-Copy types, different from this Copy example.
Concept Snapshot
Copy types in Rust allow values to be duplicated on assignment.
Syntax: let y = x; copies x if x's type implements Copy.
Both variables can be used independently after.
Common Copy types: integers, booleans, floats.
Non-Copy types move ownership instead of copying.
Full Transcript
This example shows how Rust handles Copy types. When we declare x as 5 (an i32, which is Copy), and then assign y = x, Rust copies the value of x into y. Both x and y hold the value 5 independently. This means we can use both variables after the assignment without errors. The execution table traces each step: declaring x, assigning y, printing both, and ending the program. The variable tracker shows how x and y change over time. Key moments clarify why x remains valid after assignment and what would happen if the type was not Copy. The quiz tests understanding of when y gets its value and ownership rules. Remember, Copy types duplicate values on assignment, unlike non-Copy types which transfer ownership.

Practice

(1/5)
1. Which of the following types in Rust is a Copy type by default?
easy
A. A String
B. An integer like i32
C. A vector like Vec<i32>
D. A struct with a String field

Solution

  1. Step 1: Identify primitive types with Copy trait

    Primitive scalar types like integers (i32) implement Copy by default.
  2. Step 2: Understand ownership of heap data

    Types like String and Vec own heap data and do not implement Copy automatically.
  3. Final Answer:

    An integer like i32 -> Option B
  4. Quick Check:

    Primitive types = Copy [OK]
Hint: Primitive scalars like i32 are Copy by default [OK]
Common Mistakes:
  • Thinking String is Copy because it looks simple
  • Assuming all structs are Copy without checking fields
  • Confusing heap-allocated types with Copy types
2. Which syntax correctly derives the Copy trait for a struct in Rust?
easy
A. struct Point { x: i32, y: i32 } // Copy is automatic
B. #[derive(Clone)] struct Point { x: i32, y: i32 }
C. #[derive(Copy, Clone)] struct Point { x: i32, y: i32 }
D. #[derive(Copy)] struct Point { x: i32, y: i32 }

Solution

  1. Step 1: Recall trait derivation rules

    To make a struct Copy, you must derive both Copy and Clone traits.
  2. Step 2: Check each option

    #[derive(Copy, Clone)] struct Point { x: i32, y: i32 } correctly derives both traits. #[derive(Clone)] struct Point { x: i32, y: i32 } misses Copy. struct Point { x: i32, y: i32 } // Copy is automatic is wrong because Copy is not automatic for structs. #[derive(Copy)] struct Point { x: i32, y: i32 } misses Clone.
  3. Final Answer:

    #[derive(Copy, Clone)] struct Point { x: i32, y: i32 } -> Option C
  4. Quick Check:

    Copy requires Copy + Clone traits [OK]
Hint: Always derive both Copy and Clone for Copy types [OK]
Common Mistakes:
  • Deriving only Copy without Clone
  • Assuming Copy is automatic for structs
  • Forgetting to derive Clone along with Copy
3. What will be the output of this Rust code?
#[derive(Copy, Clone)]
struct Color(u8, u8, u8);

fn main() {
    let c1 = Color(255, 0, 0);
    let c2 = c1;
    println!("{} {} {}", c1.0, c1.1, c1.2);
}
medium
A. 255 0 0
B. Compile error: use of moved value
C. 0 0 0
D. Runtime panic

Solution

  1. Step 1: Understand Copy trait effect on assignment

    Since Color derives Copy, assigning c1 to c2 copies the data, not moves it.
  2. Step 2: Check usage after assignment

    Because c1 is still valid, printing its fields outputs the original values.
  3. Final Answer:

    255 0 0 -> Option A
  4. 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

  1. Step 1: Check trait derivations

    The struct derives only Copy, but Copy requires Clone to be implemented too.
  2. Step 2: Understand compiler requirements

    Rust compiler will error because Clone is missing, even if Copy is derived.
  3. Final Answer:

    Missing Clone trait derivation causes compile error -> Option D
  4. 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

  1. Step 1: Check field types for Copy trait

    Fields u32 and tuple of u8 are all Copy types.
  2. 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.
  3. Final Answer:

    Derive Copy and Clone for Pixel since all fields are Copy types -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard