การทำลายและ __del__
ล้างข้อมูลเมื่ออายุการใช้งานของค่าสิ้นสุด
การทำลายและ __del__ เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Every Value Has an End
When a value is no longer needed, its life ends and Mojo destroys it. This is your chance to clean up what it owned. 🧹
The Destructor Method
Mojo calls a special method named __del__ to destroy a value. It runs automatically when the value goes out of scope.
fn __del__(owned self):
passDeterministic Timing
Mojo destroys values at a predictable point, as soon as they are last used. There is no waiting on a garbage collector.
Last Use, Not Block End
Mojo runs __del__ right after a value is last used, often before the block closes. This frees resources as early as possible.
Free What You Own
If your type holds a heap buffer, __del__ is where you release it. Doing so prevents memory from leaking over time.
You Own the Cleanup
Inside __del__ the value is owned, so you can safely tear down its resources. After this, the value no longer exists.
Often Synthesized
For simple types you write nothing. Mojo synthesizes a destructor that cleans up each field in turn, so leaks are avoided.
When to Customize It
Write your own __del__ when you manage a raw resource, like an allocation or file handle, that needs explicit release.
Runs Once, Exactly
Each value is destroyed exactly once. Moves transfer ownership so only the final holder runs __del__, avoiding double frees.
No Use After Death
Once __del__ completes, Mojo guarantees the value is gone. Its ownership rules stop you from touching freed data.
Safety Without a GC
Deterministic destruction gives you C-like control with built-in safety. Cleanup is timely, predictable, and automatic.
Quick Check
Lets check destruction.
Recap
__del__ runs when a value’s life ends, deterministically and exactly once, letting you clean up safely without a GC. 🎯
คำถามที่พบบ่อย
บทเรียน “การทำลายและ __del__” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำลายและ __del__” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำลายและ __del__”
ล้างข้อมูลเมื่ออายุการใช้งานของค่าสิ้นสุด คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การทำลายและ __del__” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ซีแมนติกส์ของการคัดลอกเทียบกับการย้าย
- เมธอด __copyinit__
- เมธอด __moveinit__
- การทำลายและ __del__