0Pricing
Mojo Academy · Ders

__moveinit__ Metodu

Türünüzün nasıl taşınacağını tanımlayın.

__moveinit__ Metodu, CoddyKit'te ücretsiz bir Mojo Academy dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Mojo Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Mojo Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Defining How to Move

To move a value, Mojo calls a special method named __moveinit__. It builds a new instance by taking over an old one. 🚚

It Runs on Transfer

When you use the caret transfer operator, __moveinit__ fires. It relocates the value instead of duplicating its data.

var b = a^   # calls __moveinit__

The Method Signature

It receives the new instance and the owned source value. Because the source is owned, you may consume it directly.

fn __moveinit__(out self, owned existing: Self):
    self.ptr = existing.ptr

Steal, Do Not Copy

The trick is to transfer resources like a heap pointer from the source to the new instance, rather than allocating again.

No Double Free

After the move, the source must not free the data it gave away. Mojo handles this so the buffer is owned by exactly one instance.

Cheap for Big Values

Because __moveinit__ shuffles ownership instead of cloning bytes, moving a huge buffer costs almost nothing. That is the speed win.

Source Becomes Invalid

Once moved from, the source is consumed. Mojo stops you from reading it, guaranteeing you never touch relocated data.

Often Derived for You

Like copying, you usually do not write this. Conforming to Movable lets Mojo synthesize a field-wise __moveinit__ for you.

When to Write It Yourself

Customize __moveinit__ when your type owns a resource that needs explicit hand-off, such as a manually managed pointer.

Pairs with Copy

A type can define both copy and move. Mojo then picks the cheaper one, choosing a move whenever the source is no longer needed.

Enables Cheap Ownership

__moveinit__ is what makes ownership transfer fast and safe. It lets values flow through your program without wasteful duplication.

Quick Check

Lets check the move initializer.

Recap

__moveinit__ defines how your type moves, handing resources to a new instance cheaply and consuming the source. 🎯

Sıkça Sorulan Sorular

“__moveinit__ Metodu” dersi ücretsiz mi?

Evet — “__moveinit__ Metodu” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Mojo Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Mojo Academy kursu toplamda 4 dersten oluşur.

“__moveinit__ Metodu” dersinde ne öğreneceğim?

Türünüzün nasıl taşınacağını tanımlayın. Mojo Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Mojo Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Mojo Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.

“__moveinit__ Metodu” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Mojo Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Mojo Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Kopyalama ve Taşıma Anlambilimi
  2. __copyinit__ Metodu
  3. __moveinit__ Metodu
  4. Yok Etme ve __del__
← Mojo Academy Sayfasına Dön