0Pricing
Mojo Academy · レッスン

structの定義

フィールドを1つの型にまとめます

「structの定義」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「structの定義」で何を学びますか?

フィールドを1つの型にまとめます ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「structの定義」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. structの定義
  2. フィールドと__init__メソッド
  3. Structへのメソッド追加
  4. StructとPythonクラスの違い
← Mojo Academyに戻る