Menu
CoddyTech

Derived Value Semantics

Lesson 40 of 48 in Coddy's Rust for GDScript Developers course.

GDScript class instances have identity, so two separately created objects remain distinct even when their fields match. The source supplies a named comparison method to express value equality explicitly. Rust derive generates selected trait implementations when all fields meet their requirements. Derived PartialEq compares fields, Debug prints a diagnostic representation, and Copy permits implicit copying of eligible values. The Rust assignment preserves first because this bool-only Flag is Copy. An owned String field would prevent Copy, while Clone could still provide explicit duplication. Choose the required semantics deliberately rather than assuming all objects share one equality or copying rule.

GDScript: complete program

extends SceneTree
class Flag:
    var active: bool
    func _init(value: bool):
        active = value
    func same_value(other: Flag) -> bool:
        return active == other.active

func _init():
    var first = Flag.new(true)
    var second = Flag.new(true)
    print(first.same_value(second))
    print(is_same(first, second))
    quit()

Rust: complete program

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct Flag { active: bool }
fn main() {
    let first = Flag { active: true };
    let second = first;
    println!("{}", first == second);
    println!("{:?}", second);
}

Copy is restricted value copying, not a request to deep-copy arbitrary object graphs. Diagnostic formatting is not a stable data format.

challenge icon

Challenge

Easy

In key.rs, define Key with public row: i32 and column: i32 fields and derive Copy, Clone, Debug, PartialEq, Eq, and Hash. Equality and hashing must use both fields. The derived debug representation is Key { row: n, column: n }. Implement unique::count(keys: &[Key]) -> usize, returning the number of distinct coordinate pairs without modifying the input. solve reads complete whitespace-separated integer pairs, each integer between -1000 and 1000. Return the distinct-pair count; empty input returns 0.

Implement the required exported APIs and return the result from exercise::solve(input: &str) -> String without a final newline. Do not print in editable code. The locked driver checks required APIs independently, reads all stdin, and prints the returned text with one final newline.

Your code goes in exercise.rs, key.rs, unique.rs. main.rs holds the supplied input/output code and cannot be edited.

Try it yourself

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod key;
mod unique;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // BEGIN PUBLIC API PROBES
use std::collections::HashSet;
let a = key::Key { row: 13, column: -6 }; let mut b = a; b.column = 7;
assert_eq!(a.column, -6); assert_ne!(a, b); assert_eq!(a, a.clone());
assert_eq!(format!("{:?}", a), "Key { row: 13, column: -6 }");
let c = key::Key { row: 14, column: -6 };
let mut set = HashSet::new(); set.insert(a); set.insert(a.clone()); set.insert(b); set.insert(c);
assert_eq!(set.len(), 3);
assert_eq!(unique::count(&[a, b, c, a, b]), 3);
assert_eq!(unique::count(&[]), 0); assert_eq!(unique::count(&[a, a]), 1);
assert_eq!(unique::count(&[c, a]), 2);
    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Rust for GDScript Developers

Practice on your own: Online Rust compiler