ตัวดำเนินการโอนค่า
ย้ายค่าด้วยสัญลักษณ์ ^
ตัวดำเนินการโอนค่า เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Move, Don't Copy
Passing to an owned parameter usually makes a copy. But if you are done with the original, copying is wasteful. You can move it instead. 🚚
Meet the Transfer Operator
Mojo's transfer operator is the caret ^ sigil. You place it after a value to hand its ownership over instead of duplicating it.
Using It in a Call
Add ^ when calling a function with an owned parameter. The value transfers in, and no copy is made.
var msg = String("hi")
consume(msg^)The Original Is Gone
After a transfer, the source variable is no longer valid. Mojo ends its lifetime, so you cannot accidentally use the moved value.
The Compiler Enforces It
Try to read a variable after moving it and the compiler rejects your code. This check stops use-after-move bugs before they run.
Why Moving Is Faster
A move just transfers ownership of the existing data. There is no deep copy, which matters a lot for big strings, lists, or buffers.
Copy or Move, Your Choice
Without ^ Mojo copies into an owned parameter; with ^ it moves. You decide based on whether you still need the original.
Transfer on Assignment
The caret also works between variables. Writing one var from another with ^ moves the value rather than copying it across.
var a = String("data")
var b = a^ # a is now invalidPairs With owned
The transfer operator is the natural partner of owned parameters. Together they express giving a value away cleanly and cheaply.
Safety Plus Speed
Moves give you C-like efficiency while the compiler guarantees you never touch a value after handing it off. That is safe and fast.
The Full Ownership Picture
borrowed, inout, owned, and the ^ transfer together let you control exactly how data flows through your program, with no surprises.
Quick Check
Let us check what the ^ operator does to the source.
Recap
The ^ transfer operator moves a value instead of copying it, ending the source's life. Paired with owned, it makes giving data away fast and safe. 🎯
คำถามที่พบบ่อย
บทเรียน “ตัวดำเนินการโอนค่า” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวดำเนินการโอนค่า” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวดำเนินการโอนค่า”
ย้ายค่าด้วยสัญลักษณ์ ^ คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวดำเนินการโอนค่า” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การยืมอาร์กิวเมนต์แบบอ่านอย่างเดียว
- การเปลี่ยนค่าในตำแหน่งเดิมด้วย inout
- การรับความเป็นเจ้าของด้วย owned
- ตัวดำเนินการโอนค่า