0Pricing
Mojo Academy · Lektion

Eine struct definieren

Bündeln Sie Felder zu einem Typ.

Eine struct definieren ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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__. 🎉

Häufig gestellte Fragen

Ist die Lektion „Eine struct definieren“ kostenlos?

Ja — der vollständige Text von „Eine struct definieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Eine struct definieren“?

Bündeln Sie Felder zu einem Typ. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Mojo Academy zu starten?

Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Eine struct definieren“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?

Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Eine struct definieren
  2. Felder und die __init__-Methode
  3. Einer struct Methoden hinzufügen
  4. Structs und Python-Klassen im Vergleich
← Zurück zu Mojo Academy