Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to derive the Copy trait for the struct.
Rust
#[derive([1], Clone)] struct Point { x: i32, y: i32, }
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to derive Copy causes move semantics instead of copy.
Deriving Debug instead of Copy does not enable copying.
✗ Incorrect
The Copy trait allows the struct to be copied by value. It must be derived explicitly along with Clone.
2fill in blank
mediumComplete the code to copy the value of a Copy type variable.
Rust
let a = 5; let b = [1]; println!("{}", b);
Attempts:
3 left
💡 Hint
Common Mistakes
Using &a copies a reference, not the value.
Using *a causes a syntax error because a is not a pointer.
✗ Incorrect
For Copy types like integers, assigning 'a' to 'b' copies the value.
3fill in blank
hardFix the error in copying a non-Copy type by adding the correct trait.
Rust
#[derive(Clone, [1])] struct Data { value: String, } let d1 = Data { value: String::from("hello") }; let d2 = d1;
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to derive Copy on a struct with non-Copy fields causes a compile error.
Deriving Debug does not fix move errors.
✗ Incorrect
Adding Copy allows the struct to be copied instead of moved, but only if all fields are Copy.
4fill in blank
hardComplete the code to create a Copy type struct and copy its instance.
Rust
#[derive([1], Clone)] struct Color { red: u8, green: u8, blue: u8, } let c1 = Color { red: 255, green: 0, blue: 0 }; let c2 = c1; println!("{} {} {}", c2.red, c2.green, c2.blue);
Attempts:
3 left
💡 Hint
Common Mistakes
Using &c1 copies a reference, not the value.
Using *c1 causes a syntax error because c1 is not a pointer.
✗ Incorrect
Deriving Copy allows the struct to be copied by value. Assigning c1 directly copies it.
5fill in blank
hardFill both blanks to implement Copy and clone a simple struct correctly.
Rust
#[derive([1], [2])] struct Number { value: i32, } let n1 = Number { value: 10 }; let n2 = n1; println!("{}", n2.value);
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to derive Copy causes move semantics.
Using &n1 copies a reference, not the value.
✗ Incorrect
Deriving Copy and Clone allows direct copying. Assigning n1 directly copies it.
