تولّي الملكية باستخدام owned
انقل قيمة إلى داخل دالة
تولّي الملكية باستخدام owned درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
A Third Way to Pass
You have borrowed for reading and inout for editing. The third convention lets a function take over a value entirely. 📦
Meet owned
An owned argument transfers ownership into the function. The function now holds the value as its very own to use or store.
Marking the Parameter
You write owned before the parameter name. Inside, the function has full control: it can mutate, store, or consume the value.
fn consume(owned name: String):
print(name)The Function Becomes the Owner
With owned, the value's lifetime moves into the function. When the function ends, it is responsible for destroying the value.
Why Take Ownership?
Use owned when a function needs to keep the value, like storing it in a struct or a collection, rather than just reading it briefly.
Owned Means Mutable Too
Since the function owns the value, it may freely change it. There is no caller to protect, because the value now belongs to the function.
How the Value Arrives
By default Mojo gives an owned parameter a fresh copy, so the caller's original stays intact unless you ask to move it.
Avoiding the Copy
If you no longer need the original, you can hand the value over instead of copying it. That transfer is the topic of the next lesson.
Clear Intent
Choosing owned tells readers this function will keep the value. The signature documents the data's journey at a glance.
The Three Conventions
Now you have all three: borrowed reads, inout mutates in place, and owned takes the value over. Each maps to a clear intent.
Pick by Responsibility
Ask who should be responsible for the value after the call. If the answer is the function, reach for owned.
Quick Check
Let us check what owned means for a function.
Recap
owned hands a value's ownership to the function, which can keep, change, or destroy it. Use it when the function must own the data. 🎯
الأسئلة الشائعة
هل درس «تولّي الملكية باستخدام owned» مجاني؟
نعم — نص درس «تولّي الملكية باستخدام owned» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «تولّي الملكية باستخدام owned»؟
انقل قيمة إلى داخل دالة تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «تولّي الملكية باستخدام owned»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- استعارة الوسيطات للقراءة فقط
- التغيير في المكان باستخدام inout
- تولّي الملكية باستخدام owned
- عامل النقل