0Pricing
Mojo Academy · درس

تعريف بنية struct

اجمع الحقول في نوع واحد

تعريف بنية struct درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why You Need a struct

When values belong together, like a point's x and y, you can bundle them into one custom type. In Mojo that bundle is a struct. 📦

The struct Keyword

You define a new type with the struct keyword, a name, and a colon. Everything indented under it belongs to that type.

struct Point:
    pass

Naming Your Type

Struct names use CapitalizedNames by convention, so Point or BankAccount. This makes your types easy to spot in code.

struct BankAccount:
    pass

Fields Hold the Data

Inside a struct you list its fields, the named pieces of data it stores. Each field is declared with var and a type.

struct Point:
    var x: Int
    var y: Int

Every Field Needs a Type

Mojo structs are static, so each field must state its type up front. That lets the compiler lay out memory efficiently.

var name: String
var age: Int

A struct Is a Blueprint

A struct definition is just a blueprint. It describes the shape of data but does not create any actual value on its own yet.

Instances Are the Real Thing

From one blueprint you make many instances, each with its own values. The struct is the mold; instances are what comes out of it.

Grouping Beats Loose Variables

Without a struct you juggle separate x and y variables everywhere. A struct keeps related data together, so it travels as one unit.

Structs Are Value Types

In Mojo a struct is a value type. Assigning it copies the data, which keeps behavior predictable and fast by default.

A Complete Mini Struct

Here is a full struct with two fields. This single definition now describes every Point your program will ever create.

struct Point:
    var x: Int
    var y: Int

Read It Top to Bottom

Read a struct as: here is a new type, and here is the data it carries. That mental model carries you through the whole course.

Quick Check

Let us check your grasp of defining a struct.

Recap

You met the struct: a blueprint that bundles typed fields into one value type. Next you will fill it with real data using __init__. 🎉

الأسئلة الشائعة

هل درس «تعريف بنية struct» مجاني؟

نعم — نص درس «تعريف بنية struct» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.

ماذا ستتعلم في «تعريف بنية struct»؟

اجمع الحقول في نوع واحد تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟

لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «تعريف بنية struct»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟

نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تعريف بنية struct
  2. الحقول ودالة __init__
  3. إضافة دوال إلى بنية
  4. البنى مقابل فئات Python
← العودة إلى Mojo Academy