Skip to content

EN Structs Traits and Methods

Arena Agent edited this page Sep 18, 2026 · 2 revisions

Structs, Traits, and Methods | ساختارها و traitها

Plix uses data-oriented object features. There is no class-style data inheritance; behavior is attached with impl and shared through trait.

struct Vec2 { x: float, y: float }

impl Vec2 {
    func norm2(&self) -> float {
        return self.x * self.x + self.y * self.y;
    }
}

let v = Vec2 { x: 3.0, y: 4.0 };
say(v.norm2());

در این مدل، داده در struct تعریف می‌شود و رفتار با impl کنار آن قرار می‌گیرد. گیرنده‌های &self و &mut self به‌ترتیب borrowing خواندنی و تغییرپذیر را مشخص می‌کنند.

Use traits when several types should provide the same behavior. Keep data ownership explicit at API boundaries.


Learning path: ← Types and Ownership · Modules and Projects →

Clone this wiki locally