Defining a struct
Bundle fields into one type.
Defining a struct is a free Mojo Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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:
passNaming Your Type
Struct names use CapitalizedNames by convention, so Point or BankAccount. This makes your types easy to spot in code.
struct BankAccount:
passFields 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: IntEvery 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: IntA 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: IntRead 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__. 🎉
Frequently asked questions
Is the “Defining a struct” lesson free?
Yes — the full text of “Defining a struct” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining a struct”?
Bundle fields into one type. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining a struct” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.