Skip to content

Instantly share code, notes, and snippets.

@Zakarialabib
Last active June 24, 2023 11:37
Show Gist options
  • Save Zakarialabib/26dbfe873ec1d23a2f9efb03663ad6e8 to your computer and use it in GitHub Desktop.
Save Zakarialabib/26dbfe873ec1d23a2f9efb03663ad6e8 to your computer and use it in GitHub Desktop.
PHP UK Conference 2023: Rust For PHP Developers

PHP UK Conference 2023: Rust For PHP Developers Transcribition for reader in both arabic & english

حسنًا ، هل يمكن للجميع سماعي على ما أعتقد حتى في الخلفية ، وهو أمر رائع

مرحبًا به لمطوري PHP لأولئك الذين لا يعرفونني ، اسمي Nuno مبتكر أدوات متعددة مفتوحة المصدر على نظام PHP الإيكولوجي مثل Pest Collision Larastan

والمزيد من مشروعي الأساسي إلى جانب العمل من أجل Laravel PHP إذا كنت لا تعرف ما هو إلى حد

كبير إطار اختبار PHP أنيق

مع التركيز على البساطة لذلك إذا كنت

مهتمًا بذلك ، فانتقل إلى passphp.com وأخبرني برأيك

الآن ،

آمل أن يكون الجميع متحمسًا لأنهم

سيتحدثون عن الراحة من

منظور مطور PHP وسنقوم في

الواقع بالعيش الكامل جلسة

الكود ولكن قبل أن ننتقل إلى ذلك ، دعني

أقدم لك القليل من تاريخ

الصدأ

يعتقد معظم الناس أن الصدأ تم إنشاؤه

بواسطة Modzilla ولكن في الواقع تم إنشاء روس

بواسطة موظف Mozilla باسم

Graden كمشروع موقع عادي و

كان Graden يقوم بالكثير من C in C plus في

ذلك الوقت ، وقال شيئًا كهذا

عند إنشاء المشروع ،

صفقاتي الرئيسية للحديد الصدأ

محبطون من مطوري C plus plus الذين

اختاروا C plus و Duo لأدائها

لكنهم يحبون ذلك احصل على شيء أكثر أمانًا

الآن إذا كنت تعمل مع لغات أنظمة

مثل C أو C بالإضافة إلى أنك تواجه هذا

النوع من الأشياء طوال الوقت ، أو

خطأ تجزئة مزدوج أو فائض في المخزن المؤقت للفساد ،

فهذا إلى حد كبير ما

تواجهه طوال الوقت ولم يكن التقدير

سعيدًا حول هذا الأمر ،

فإن ما يفعله هو أنه

جعل الصدأ لغة آمنة للذاكرة

لمحاربة الأخطاء الأخرى التي رأيناها للتو

من قبل

ولكن مع نفس المزايا مثل C

و C plus وهو الأداء المعدني العاري

الآن

Mozilla التي كانت الشركة

كان التصنيف يعمل في الوقت الذي

رأوا فيه أيضًا القيمة في هذا الأمر ، حيث اعتقدوا

أننا نستخدم الكثير من الصدأ ونستخدم الكثير من

C و C plus داخل Mozilla

ونواجه هذا النوع من المشكلات المتعلقة

بأمان الذاكرة طوال الوقت في Firefox

على سبيل المثال ، لذلك قالوا إن الدرجات

تعمل فقط على ذلك الوقت الكامل داخل Mozilla

ودعنا نرى المزيد من الصدأ داخل Mozilla

نفسها ، وقد

حدث ذلك بالفعل خلال 10

سنوات.

حصل فريق الصدأ على هذا الحظر أو الإلغاء داخل أم

موزيلا الآن في عام 2020 ، كانت الكثير من الشركات

تستخدم بالفعل الكثير من الصدأ

وأنشأوا هذا الشيء المسمى مؤسسة الصدأ

وهذه مؤسسة المعصم

مدعومة من قبل شركات صغيرة جدًا كما

يمكنك أن تتخيل مثل AWS Google meta و

Microsoft شركات كبيرة جدًا لديها

أموال طائلة ترعى

مؤسسة الصدأ في ضمان أن الصدأ

يمضي قدمًا معها

ولإعطائك منظورًا لمدى

استثمار هذه الشركات في Rust ،

هذا رسم بياني لإصدار Android

منتجًا من Google وهذا الرسم البياني

يمثل

نقاط الضعف المتعلقة بسلامة الذاكرة التي يواجهها برنامج Android

سنويًا ، ويمكننا أن نرى أنه في عام

2019 ، يواجه مشروع Android أكثر

من 200 نقطة ضعف تتعلق بسلامة الذاكرة

في غضون أربع سنوات ، وبعد ذلك يكون

الرسم البياني الزمني هنا في الأسفل يمكننا رؤيته أن

برنامج Android يحتوي الآن على أقل

من 100 نقطة ضعف في أمان الذاكرة ،

لذا خمن ما الذي تغير خلال تلك

السنوات الأربع من الوقت الذي

بدأوا فيه بالفعل في تنفيذ المزيد من الصدأ داخل

برنامج Android والآن

لديهم نفس القدر من الراحة في

C رمز داخل منتج Android

أجنبي

حتى أن Google قالت إنه

تم اكتشاف ثغرات أمنية في الذاكرة الصفرية في

رمز صدأ Android ، لذا يتم شحن كل الكود الجديد

في بقية الأجزاء ، وكلها لا تحتوي على

ثغرات أمنية في الذاكرة وأيضًا

أعضاء مسيطرون في

مجتمع JavaScript وهو Ryan المنشئ من

العقدة في Dino كلا وقتَي تشغيل

نظام JavaScript البيئي ، قال إنني أمتلك

أكثر متعة في كتابة الصدأ هذه الأيام

ولن أبدأ مشروع c plus مرة أخرى ،

لذلك هذا مستخدم C plus plus بكثافة

والذي يقول إنني لن أستخدم C plus

plus مرة أخرى

الآن إلى جانب سلامة الذاكرة التي

يجلبها الصدأ إلى المشاريع ، فهو أيضًا يحسن

تجربة المطور كثيرًا ، هناك

هذا الشيء الذي يطلق عليه

مسح مطور تجاوز المخزون الذي يتم إجراؤه

كل عام

والباقي للسنة السابعة على

التوالي لغة البرمجة الأكثر محدودية

في العالم مع ثمانين

بالمائة من الانتظار ، أكثر من 80 بالمائة

من المطورين الذين يعملون مع الصدأ

يريدون الاستمرار في العمل مع الراحة الآن

لأن هذا مؤتمر PHP

اعتقدت أنه من المضحك أن أرى في الواقع

مكان PHP في كل هذا

جيدًا

الآن أن نكون صادقين حقًا

يختلف قليلاً لأن الأشخاص الذين بدأوا في

العمل مع الصدأ بدأوا على الفور

مع اللغة الحديثة مع كل هذه

الميزات التي يمتلكها الصدأ ومع PHP

ليس عادلاً ، لذا فإن PHP لديها 40 بالمائة من

المطورين الذين يعملون مع PHP لا يزالون تريد

الاستمرار في العمل مع PHP وهذا ما

يحدث في رأيي ،

لأنه ليس كل شخص محظوظ بما يكفي

للعمل مع PHP الحديثة ، لذلك لدينا الملحن

PHP Stan Peach Punit في الماضي وليس

الجميع محظوظين بما يكفي للعمل مع

PHP الحديثة هناك لا يزال الكثير من

مطوري PHP الذين يعملون مع مشاريع Legacy

الآن بعد أن يقال ، دعنا ننتقل إلى بعض

الأكواد هنا ، لذا فإن ما سنفعله

اليوم هو أننا سنقوم

بتطبيق CLA بسيط في وقت الراحة

والذي سيكون مهمة قائمة في CLI

حيث يمكن للأشخاص إضافة مهام ثم

عرض جميع المهام على ما يرام ، لذلك

سأعرض عليك تطبيقًا بسيطًا جدًا للراحة المنتظمة وسأعرض

لك هذا العرض الذي يعرض لك بعض

العناصر المكافئة في PHP حتى يتمكن الجميع من

التعرف عليها والبدء بها

حسنًا ، سأنتقل هنا إلى

الجهاز الطرفي الخاص بي في Sublime Text الذي هو

المحرر المفضل لدي ، ثم لدي هذا لعمل نسخة

احتياطية في حالة تعطل الأمور عن القضبان ،

وآمل ألا يكون ذلك

أول شيء سنفعله هو

التثبيت الصدأ على جهازي ، فقط ضع في

اعتبارك أن التثبيت الإجمالي على

جهازي يعمل بنفس الطريقة التي يتم بها

تثبيت الصدأ على

نظام التشغيل Ubuntu أو نظام التشغيل Windows وما

عليك فعله هو كتابة هذا الأمر الذي

يسمى هذا البرنامج النصي تم معالجته جيدًا

ويمكنني تنزيله إنه مع curl على سبيل

المثال وتشغيله وما يفعله الباقي هو

تثبيت ثنائيتين على

جهازي أحدهما هو الصدأ C والآخر

هو Cargo بخير ، لذا سنحصل على

هاتين الأداتين الصدأ C والبضائع بمجرد

تشغيل هذا البرنامج النصي

بمجرد تشغيله ، نحصل على إمكانية الوصول إلى Cargo and

cargo لنمنحك ما يعادله على PHP

هو إلى حد كبير مؤلف الصدأ مع

هذا يمكننا إضافة التبعيات وإزالة

التبعيات وحتى بدء مشروع جديد ،

لذا فهذه هي الأداة الأولى التي نحصل عليها

بعد الشحن نحصل أيضًا على شيء يسمى

العنوان C والصدأ C

يعادل إلى حد كبير ثنائي PHP

بمعنى أنه مع الإنقاذ تقوم

بالفعل بترجمة الكود الذي يمكنك تشغيله

بعد ذلك ، وباستخدام PHP تقوم بتفسير

وتشغيل الكود ، لذلك هناك

جزء مكافئ في لغات مختلفة

الآن مرة أخرى مع البضائع ، يمكنك إضافة

التبعيات ، وإزالة التبعيات ،

أو بدء مشروع جديد ، يمكنك فعل

الشيء نفسه مع الملحنين ، وإنشاء

مشروع ، لذلك دعنا نفعل ذلك سأقوم

بكتابة البضائع التي تحتاج إلى القيام بها الآن شيء واحد يجب أن

تضعه في الاعتبار هنا هو أنه إذا

قمت بذلك فقط على هذا النحو ، فسأنشئ

تطبيقًا ثنائيًا جديدًا ، ولكن إذا مررت

خيار شرطة شرطة ، سأقوم

بإنشاء حزمة جديدة مثل مكتبة

بشكل أساسي يمكن للآخرين استخدامها في هذه الحالة.

سأقوم بإزالة حافة اندفاعة الشرطة

وسأقوم ببساطة بكتابة البضائع التي

نحتاج إلى القيام بها في هذا لأن التعليقات الواردة من

المحطة تخبرني أننا

سننشئ تطبيقًا ثنائيًا

الآن ، دعنا نلقي نظرة على

بنية الملف هنا ، لذا سأفتح هذا

لأفعله على يساري وسأفتح

هذا الملف باسم

cargo.tomel file ، حيث إن

cargo.com تعادل

composer.json مما يعني أن لدينا كل

البيانات الوصفية معلومات حول تطبيقنا

في هذه الحالة ، من الواضح أن لدي

اسم تطبيقي وهو

إصدار تطبيقي وهو 0.1

على الجانب المكافئ في PHP ، ونحن في

الواقع نضع علامة على استخدام git tag

لمعرفة إصدار الحزمة

ولكن عند استخدام Rust إصدار العلم هذا

ثم لدينا إصدار الصدأ ، أما

الإصدار الباقي فهو مكافئ

لإصدارات PHP لدينا php5 php7 php8 ولكن

مع الصدأ ، نعمل مع الإصدارات بشكل جيد

وهذا هو الإصدار المطلوب

لتشغيل مشروع الصدأ الخاص بي

ثم في الجزء السفلي I لديك قائمة

التبعيات هذه المشابهة

للتبعيات المطلوبة للملحن حيث يمكنني

تحديد تبعيات المجتمع التي

يستخدمها مشروع الراحة الخاص بي ،

ويمكنني المجيء إلى هنا والقيام بشيء مثل

Chrono يساوي

4.0.0 وهذا يعني ببساطة أن

مشروعي يستخدم المجتمع مكتبة التاريخ والوقت من

الصدأ وهي Chrono

لتطبيق CLI الخاص بي ، فأنا في الواقع لا أحتاج إلى أي

تبعيات ، لذا سأبقي هذه

القائمة فارغة في الوقت

الحالي أريد أن أعرض عليك

ملف cargo.log وهو ملف آخر ولهذا

سأقوم بتجميع مشروعي لأول

مرة ، ولكي أفعل ذلك ، سأقوم

ببساطة بكتابة تشغيل البضائع

لأرى أنني بحاجة للدخول في المجلد

أولاً

ثم سأقوم بكتابة تشغيل البضائع الآن

عندما أكتب البضائع تشغيل أول شيء

أراه في الأعلى هو خطوات التجميع

لمشروعي حتى أتمكن من رؤية أن مشروعي

قد تم تجميعه ثم الانتهاء من التجميع

وبعد ذلك هو نوع من تنفيذ

الثنائي نتيجة التجميع

ويمكنني رؤية Hello World على وحدة التحكم الخاصة بي

وهو بالضبط ما يخبرني به كود المصدر الخاص بي

الآن بمجرد تشغيل البضائع لأول مرة

يمكنني بالفعل تشغيل قائمة انتظار cargurun Dash

وهي طريقتي المفضلة لتشغيل

البضائع والتي تخفي بشكل أساسي جميع

خطوات التجميع التي أراها هنا في الأعلى.

يطبع بالضبط ما يطبعه برنامجي ،

لذا بمجرد تشغيل البضائع لأول مرة

أحصل على هذا الملف وهو cargo.lock

وهذا إلى حد كبير يعادل

الملحن.

ثم لدي أيضًا هذا المجلد الهدف

الذي يحتوي على جميع

الملفات المؤقتة الناتجة عن

التجميع ، وأريد منك نوعًا ما أن تتجاهل

هذا المجلد في الوقت الحالي ، فقط ضع في اعتبارك أن

هذا المجلد الهدف يمثل بعض

الملفات المؤقتة المستخدمة في عملية

التجميع

ثم لدينا دليل المصدر الخاص بنا بحيث يكون

مكافئًا مسبقًا لـ PHP ولكن في

دليل المصدر هذا لدينا

ملف main.rs الخاص بنا ، يعد main.rs

مكافئًا لملف index.php العام

وهو نقطة دخول تطبيقك

ويمكنني أيضًا رؤية ذلك في هذا

main.rs ، لدي وظيفة بالاسم

main وهي نقطة دخول تطبيقي على حد سواء

ويمكننا أن نبدأ فورًا في

فهم بعض الأشياء هنا حول Russ ،

أول شيء هو إعلان

الوظائف التي يمكنني إعلان الوظائف باستخدام

F والكلمة الأساسية بعد ذلك إعطاء

اسم الوظيفة ثم قائمة

المعلمات الآن كما ترون هنا

لم يتم تحديد النوع ، وهذا

شيء رائع عن الراحة وهو أن

الاستدلال بالنوع قوي جدًا ، لذا إذا

أردت أن أكون صريحًا يمكنني القول أن

الإرجاع نوع هذه الوظيفة باطل ، لذا فإن

هذا هو المكافئ لشيء PHP

مثل هذا بشكل أساسي ، لذا هنا هذه

الوظيفة تعيد الفراغ الذي لا يعني

شيئًا ، وبعد ذلك عندما يكون لديّ

طباعة ماكرو OLN والتي تشبه الوظيفة التي

تعرض عالم Elder وبالطبع إذا كنت

تشغيل البضائع هنا

يمكنني رؤية Hello World على وحدة التحكم الخاصة بي ،

لذا

فنحن نقوم بتطبيق CLI مرة أخرى ، لذا من

المحتمل أن يكون أول شيء نحتاج إلى القيام به

هنا هو شرح الاستخدام للمستخدم

الذي سيكون شيئًا مثل الاستخدام للقيام به

وهي الطريقة التي أستخدمها أرغب في استخدام

تطبيقي ثم القيام بذلك ثم

تمرير الأمر وأخيراً قائمة

بهذه الحجج

لا بأس بها ،

لذا من الواضح أنه إذا قمت بتشغيل البضائع ، يجب أن أرى

هذا على وحدة التحكم التي أفعلها ، فما

يمكنني فعله أدناه هو الكتابة الآن قائمة

الأوامر وقائمة

الأوامر ستكون في

الأساس شيئًا مثل هذا الأمر

يساوي أمرين بالطريقة التي

نعمل بها مع أمرين اليوم أحدهما

هو أساسًا يريد الأشخاص تشغيل

إعلان تشغيل البضائع ثم تنفيذ المهام

وصف مثل القيام بأحدهما على سبيل المثال

والآخر الذي سنقوم ببنائه

معًا هو القائمة

لعرض كل شيء قمنا بإضافته إلى

قائمة المهام ، لذا دعنا نوضح أنه

للمستخدم سيكون أمر الإضافة

الذي يتلقى الوصف

كحجة ويضيف هذا الأمر مهمة

واجبة ،

ثم أدناه مباشرة لدينا أمر القائمة

الذي يعرض بشكل أساسي

كل ما يجب فعله

إذا قمت بتشغيل هذا مع البضائع ، آمل أن

تظل الأشياء تعمل كما هو متوقع

وهي شيء يمكنني القيام به هنا

لأجعله هذا أكثر إثارة قليلاً

هو إعطاء بعض المساحة هنا ، حسنًا ، سأفعل

ذلك ، ثم أقوم بتشغيل

قائمة انتظار Dash للبضائع ، هناك نذهب

وأرى الاستخدام مباشرة على

الجهاز الطرفي

الآن بعد أن عرفنا قليلاً عن

الوظائف هل تبحث في هذه

الوظيفة الرئيسية ، وهو شيء يمكننا القيام به هو

استخراج هذا الاستخدام إلى وظيفة جديدة

مع مساعدة عرض الاسم ، لذا دعنا نفعل

ذلك العرض يساعد على إرجاع الفراغ

ومن ثم سيكون جسم الوظيفة إلى حد

كبير نسخة لصق من محتوى

السابق الوظيفة الرئيسية وهنا

سأقوم باستدعاء مساعدة العرض

لمعرفة الاستخدام بنفس الطريقة التي أفعلها

الآن

بمجرد أن يكون لدينا

الاستخدام ، سيتفاعل المستخدم بالفعل

مع هذا مع أداة CLI هذه

للقيام بشيء مثل تشغيل البضائع ثم

تمرير الأمر الذي يمكن إضافته أو

سرده ، لذا نحتاج إلى التقاط

الحجج الخاصة بتطبيق CLA هذا

الآن للقبض على الحجج ،

يمكننا في الواقع استخدام شيء مبني على

Rust ، أطلق عليه

أهدافًا من المكتبة القياسية التي

أريد التحدث عنها قليلًا عن ذلك ، لذا

أريد في الواقع التحدث عن المتغيرات

قبل ذلك ، فأنا آسف

لذلك للقبض على الأمر الذي نحتاجه لالتقاط

الحجج ثم الأمر ولكننا

نحتاج إلى تخزين هذه القيم في مكان ما وعادة ما نقوم

بتخزينها في المتغيرات

ودعنا نتحدث قليلاً عن

المتغيرات على Rust قبل أن

نتحدث بالفعل عن المكتبة القياسية ،

لذا فإن المتغيرات في Rust ليست في الواقع

متغيرات مما يعني أنها لا تغير

القيمة التي لا تتغير افتراضيًا ،

وهذا يختلف قليلاً عن PHP نظرًا

لأن المتغيرات يمكن أن تتغير ، فهي في

الواقع متغيرات ، لذا استرح عندما

أكتب شيئًا مثل أنني سأفعل

هذا على مفتاح رئيسي

عندما أكتب شيئًا مثل let command

يساوي

f

ثم أكتب أدناه الأمر

يساوي القائمة

في PHP هذا سيعمل بمعنى أن

قيمة الأمر ستتغير ، ولكن عند

الراحة بمجرد أن أحاول تصحيح ما هو

داخل الأمر ،

سترى أن الصدأ سيبدأ في

الشكوى ، لذا هناك أمران

نحتاج إلى إلقاء نظرة على أولهما

يخبرني المترجم الباقي على وحدة التحكم أنه لا يمكنك تعيين مرتين

لمتغير غير قابل للتغيير مما يعني أنني

قمت بتعيين القيمة المضافة هنا ولكن

بعد ذلك بعد أن حاولت تعيين

قيمة مختلفة للأمر المتغير

ولن يعمل ذلك في Rust لذا

المتغيرات غير قابلة للتغيير افتراضيًا الآن

بفضل هذه التجربة الرائعة للمطورين

على وحدة التحكم التي يمكننا القيام بها ، يمكننا أن

نرى هنا يمكننا أن نرى أن روس

يخبرني أنه يفكر في جعل هذا

الربط قابل للتغيير وهو ما يمكن

القيام به من خلال معنى الكلمة الأساسية كتم أنه إذا

أتيت إلى هنا وقمت فقط بكتابة

أمر مفتاح المزاج ، فسأتمكن الآن من رؤية

قيمة

الأمر الخاص بي على الجهاز وهو عبارة عن قائمة اكتب ذلك

في الأسفل ولكن الصدأ

مفيد جدًا مرة أخرى على وحدة التحكم ، إنه

يخبرني حقًا على الرغم من أنك تقوم فقط

بتعيين أمر مثل للإضافة ومن ثم

لا يكون للتجاوز أدناه

أي معنى لذلك

سأخبرك بتحذير هنا

ولكن برنامجي يجمع وهذا

بفضل الكلمة الأساسية المزاجية التي تسمح

لي بتغيير المتغيرات لذلك مرة أخرى ،

المتغيرات غير قابلة للتغيير افتراضيًا عند

السكون

والآن بعد أن عرفنا كيفية

إنشاء المتغيرات فعليًا ، يمكننا البدء في جمع

الحجج القادمة من CLI والتوضيح

حول

هذا المتغير ، سأقول أن

الأقواس عبارة عن متجه لـ السلاسل ،

هذا هو المكافئ ، المتجه في

Rust هو المكافئ للمصفوفة في

PHP ، ومع ذلك يمكننا أن نقول ما هو

داخل المتجه وهو

قيم um التي هي سلاسل ،

لذلك داخل الأقواس عبارة عن متجه

للسلاسل وهذا المتجه من السلاسل سوف

أن تكون شيئًا من المكتبة القياسية

تسمى المكتبة القياسية

ثم

أعطني الأقواس من نهاية

المكتبة القياسية

لذلك أريد أن أتحدث قليلاً عن

هذا ، فإن الصدأ القياسي للمكتبة هو

إلى حد كبير بعض المساعدين الشائعين الذين يتم

استخدامها بشكل شائع في مشاريع الصدأ حتى

في كل لغة لدينا في الواقع ، فإن هذا

يساعد إلى حد كبير الأشخاص الذين ينشئون تطبيقات

على PHP ، لدينا مثل

متغير الخادم العالمي والعالمي ، لدينا مجموعة

من وظائف المصفوفات ، ولدينا

أيضًا مكتبة التاريخ والوقت والتي تتيح

لك إلى حد كبير إنشاء تطبيقات PHP مع هؤلاء

المساعدين ، حسنًا ، لدينا هذه

المكتبة القياسية التي تتيح لي الوصول

إلى وحدات معينة ، أحدها هو

وحدة amp التي يمكننا الحصول على

الحجج من هذا بوضوح إذا حاولت

تصحيح هذا لذا سأفعل التي

آمل أن يكون لدي كل الحجج على وحدة التحكم الخاصة بي لدي

خطأ هنا يمكنني استيراد هذا

مثل هذا

ما في

Vector of strings الذي

كان يجب أن يفعل هذا ،

ما الذي يحدث بحق الجحيم ،

أعطني ثانية وهذا هو السبب في أنني صنعت لأفعل

هاه ، أحتاج إلى تسميته بجمع بالطبع ،

أنا آسف على هؤلاء الرجال ولهذا

السبب لديك نسخة احتياطية فقط

في حال تم

تجميعها الآن وفي الجزء

السفلي لدي في السطر الثامن ناقل

السلاسل الخاص بي وهو العرض

بالثنائي ، ومع ذلك يمكنني إضافة الأمر ، وقد قمت

الآن بعرض الأمر add على الجهاز الطرفي الخاص بي ،

يمكنني تمرير

وصف له على سبيل المثال ، ويمكنني

رؤية هذا الوصف أو في Vector

of strings الخاص بي

حتى تتمكن من رؤيتنا. يمكننا استخدام

dbg وظيفيًا ، وهو ما

يعادل إلى حد كبير تفريغ yfr في PHP

ويمكننا رؤية القيم التي تم تجميعها

في وقت التشغيل ويمكننا رؤية هذه القيم

على المحطة ، لذلك بمجرد أن نحصل على

الوسيطات تذكر أننا نحاول

التقاط الأمر ، لذلك عندما أكتب A

Cargo ، نحتاج إلى التقاط هذه

القيمة للقيام بشيء ما باستخدام

الأمر كما يمكنك أن تتخيل إذا كان

لدينا الأمر add ، فنحن بحاجة إلى القيام

ببعض التعليمات البرمجية إذا كانت لدينا القائمة التي نحتاجها

افعل شيئًا آخر يعرض

جميع المهام ، وبعد ذلك إذا كان لدينا

شيء لا معنى له ،

نحتاج إلى إخبار المستخدم أن

الأمر غير معروف ، لذلك دعونا نفعل ذلك ،

دعونا نلتقط الأمر عن طريق القيام

بشيء مثل الأمر let يساوي

args الحصول على الفهرس رقم واحد لأنك

تذكر أن الأمر موجود ضمن الفهرس

رقم واحد وشيء واحد يسمى Rust

والذي من المحتمل أن تتوقع رؤيته

هنا هو أنه في PHP لدينا هذا الشيء

مثل ESET إذا كان مفتاح المصفوفة موجودًا داخل

المصفوفة ثم نلتقط القيمة جيدًا

في حالة الراحة ، فلا توجد مستويات جديدة على ما يرام ، لذا

أحتاج هنا إلى أن أخبرك بالراحة

التالية لفك السلسلة داخل

المفتاح رقم واحد وإلا

فإننا نحتاج إلى تنفيذ

رد الاتصال الافتراضي هذا ، لذا سأذهب للقيام بذلك ، سأقول

حسنًا إذا كان هناك

أي شيء في الموضع الذي هو

الحال هنا ، ولكن هذا ليس هو

الحال عندما لا أقوم بتشغيل أي أمر

أريدك أن تعيدني تلك السلسلة ولكن

بخلاف ذلك ستنفذ

رد الاتصال الافتراضي هذا وعندما لا

يقدم المستخدم الأمر ، ما نحتاج إليه

هو عرض المساعدة تمامًا مثل ذلك

ثم نحتاج إلى الخروج من البرنامج مع

ملاحظات مع رمز الخروج ،

سأفعل ذلك باستخدام وحدة العملية من

الصدأ مرة أخرى ، مساعد

وسأخرج مع الكود الثابت برمز

الحالة 0 وأحتاج إلى استيراد هذه

الوحدة هنا في الأعلى حتى تكون

عملية المكتبة القياسية

جيدة ، لذا دعنا نرى كيف يحدث هذا ،

فلنصحح الأخطاء هنا أيضًا أمر

لمعرفة ما إذا كان بإمكاننا التقاط F أو القائمة ،

لذا سأذهب إلى قائمة انتظار Cargo run Dash وأرى

المساعدة التي تعني أننا

ندخل في رد الاتصال الافتراضي هذا ، فإن

المفتاح الأول غير موجود

ولكن إذا قمت بتشغيل البضائع تشغيل Dash Q F

يمكنني رؤية الإعلان على وحدة التحكم وأرى

قائمة الآن شيء رائع واحد وربما

تشعر بنفس الشعور مع

PHP Stand في المستوى Max هو أنه إذا كان

البرنامج يجمع ما يعني أنه

يعمل نوعًا ما كما هو متوقع فهذا

شعور رائع جدًا عند العمل مع

لغات المترجم مثل الباقي هو الشعور

بأنه إذا تم تجميعه فهو نوع من

العمل إلا إذا قمت بعمل خطأ مطبعي في مكان ما

وهو شعور جيد جدًا عند

العمل مع الصدأ

الآن مرة أخرى في هذه المرحلة على

متغير الأوامر لدينا القيمة المضافة لدينا

قيمة القائمة ولكن لدينا أيضًا

شيء إذا كتب المستخدم مثله

وهو لا شيء على الإطلاق ، فلدينا الأمر

بهذه القيمة التي لا

تمثل في الواقع أي شيء لا

نعرف ما يجب فعله به لذلك نحن بحاجة إلى بعض

تدفق التحكم لفعل شيء ما عندما يتم

إضافته لفعل شيء ما عندما يكون في قائمة ولإجراء

شيء ما عندما لا

يكون للقيمة أي معنى ولهذا يمكننا استخدام

تعبير المطابقة

والباقي لديه نفس الأشياء

مثل PHP لذلك نحن لديك إذا كانت الظروف الصحية

لدينا عبارة المطابقة

وهي بالضبط ما

سنستخدمه ويمكن إجراء المطابقة

بشيء مثل هذا حسنًا ،

دع كود الخروج يكون نتيجة

تعبير المطابقة التالي إذا كان

الأمر سلسلة

تساوي القيمة المضافة التي

كنت أرغب في القيام بها ضمن

هذه الكتلة من التعليمات البرمجية

إذا كان الأمر عبارة عن قائمة أريدك أن تفعل

شيئًا آخر

ولكن إذا كان الأمر شيئًا

لا أعرفه يعني أنني أريد الدخول في

هذا النطاق الافتراضي الذي أريده افعل

شيئًا آخر ، والآن بعد

أن أصبح لدينا عددًا صحيحًا

من 32 بتًا على كود الخروج ، ما يمكننا

فعله هنا هو ببساطة الخروج بكود الخروج هذا

وبالطبع نحتاج إلى الحصول على

رمز خروج من تعبير المطابقة الضخم هذا ،

لذلك نحتاج إلى الكتابة بعض الكود هنا

في الواقع ، لذا سأقوم بطباعة

سطر جديد مع إضافة مهمة

وإرجاع العدد الصحيح المكون من 32 بت

  1. الآن هناك شيء رائع جدًا عند الراحة هو

أنه إذا كان لديك شيء مثل هذا

يعني أن لديك رجوع بالإضافة إلى

فاصلة منقوطة وهي السكر الاصطناعي

الذي تقوم بإزالة الفاصلة المنقوطة

ثم يمكنك إزالة الكلمة الرئيسية

التي تبدو رائعة جدًا لذا فإن بعض

السكر الصناعي ، إذا كان لديك هذا ، فهذا

يعادل وجود

هذا ، لذا يمكنك إخفاء الفاصلة المنقوطة

وإخفاء بيان الإرجاع الذي هو في

رأيي رائعًا جدًا ، اعتدت أن أرى المزيد

من هذه الأشياء في PHP لأكون صادقًا ،

ثم يمكننا القيام بنفس الأشياء في

أمر list

بدلاً من إضافتها للقيام بذلك ، فمن

الواضح أننا نعرض كل ما يجب فعله

ونعيد صفر عند في نفس الوقت

وأخيرًا بالطبع ، إذا أدخلنا

بيانًا افتراضيًا يعني أنه ليس

لدينا إضافة أو قائمة ، نحتاج إلى إخبار

المستخدم

بأمر غير معروف

وتحتاج إلى التراجع عن

حالة الخروج 1 وهو خطأ لذلك دعنا نرى

كيف يعمل البرنامج الخاص بي ، سأقوم

بكتابة عروض

cargoon-q ، فالمساعدة منطقية ،

إضافة Dash Q التي تحتاج إلى القيام بها ثم

قائمة لعرض كل المهام التي يجب القيام بها ، وبعد ذلك

يمكنني رؤية أمر غير معروف

حتى الآن إنه يعمل كما هو متوقع باستثناء

القيام بالمهمة المناسبة لتخزين المهام

وعرض جميع الأوشام ولكن أه

أعتقد أنه يعمل بشكل صحيح

قبل أن نتحدث قليلاً

عن الكود الملموس الذي سنقوم بتخزين

تلك المهام في طبقة ثابتة

دعنا في الواقع نعيد صياغة هذه العناصر

قليلاً لأنه في هذه المرحلة يحتوي الملف الرئيسي الخاص بي على

42 سطرًا وللسبب نفسه الذي

لا تقوم فيه بتخزين الكود

الخاص بك

في الفهرس. نفس الأشياء التي نقوم

بإعادة تشكيل جميع الكود الخاص بنا بشكل أساسي عندما

يتعين علينا النظر في هذا

التطبيق على PHP ، كيف

يمكنني معالجة هذه المشكلة ، ربما سأستخدم

وحدة تحكم Symphony لهذا الأمر ، وربما يكون

لدي فئة منفصلة تحمل

اسم الأمر add to في الواقع التعامل مع

عملية تخزينه للقيام به وبعد ذلك

سأضيف أمر قائمة

لعرض جميع المهام على الجهاز الطرفي الخاص بي ،

لذلك دعونا نفعل ذلك عند الصدأ أو على الأقل نحاول

فعل الشيء نفسه ، لذا ما سأقوم

به القيام بذلك هنا

هو أنه في دليل المصدر الخاص بي ، سأقوم

بإنشاء ملف جديد سيحتوي هذا الملف على

وظيفة commands.rs

في هذا الملف ، وسأقوم بتخزين الكود

الخاص بأمر إعلاني وسأبدأ

فقط أسفل رمز أمر قائمتي

مرة أخرى ،

ليس لدينا فئات في البقية ولكن

لدينا شيء يقولون إنه ليس

مشابهًا ولكنه نوعًا ما هو لذلك يمكننا

القيام بنفس الشيء مع شيء

يسمى بهيكل

لذلك هنا سأقوم بإنشاء

شيء يسمى الأمر F الصارم

وضمن هذا الهيكل يمكنني الحصول على

خصائص مشابهة للفئات على PHP ، لذا

يمكنني هنا الحصول على شيء مثل name

يساوي سلسلة ومن ثم يمكن أن يكون لدي

على سبيل المثال العمر يساوي عدد صحيح من

32 بت هناك الكثير من أنواع dotty على

Rust مثل عدد صحيح من 32 بت أعداد صحيحة غير موقعة

وهي أعداد صحيحة موجبة

ولكن اليوم لن نتحدث

عن ذلك كثيرًا

لذا أريد أن أمر الإعلان الخاص بي لم يكن لديه

أي خصائص لـ الآن ،

ومع ذلك ، فأنا بحاجة إلى طريقة واحدة على الأقل

للتعامل مع عملية تخزين

المهام على الطبقة الثابتة ، لذلك

دعونا نضيف في الواقع بعض الطرق

لتدمير الطريقة التي يمكننا بها القيام بذلك على الصدأ

ليس ضمن تعريف الهيكل ، إنه

فقط على النطاق أدناه ، لذا ما يمكنني فعله

هنا هو شيء مثل تنفيذ

الأمر F ، ثم سأضع جميع

الطرق ضمن نطاق أوامر تنفيذ F هذا

وسأفعله

أولاً وقبل كل شيء ، إنشاء

اتفاقية المُنشئ الخاصة بي بشأن الصدأ يستخدم

اسم الوظيفة الجديد هذا ، لذا فإن الوظيفة الجديدة

ترجع الذات وهي البنية

بشكل أساسي ، إنها إلى حد كبير

PHP للهاتف الخلوي أيضًا ،

ثم داخل هذه الذات ضمن هذه

الوظيفة ، سأقوم بإرجاع

الأمر add

بدون أي خصائص. تذكر أن تذكر

هذا عندما يكون لديك فاصلة منقوطة

يمكننا إزالة الفاصلة المنقوطة وإزالة الكلمة الرئيسية

للعودة وجعل هذا الرمز

أبسط قليلاً ،

لذا فإن هذا يكافئ بنية

في PHP وأسفله مباشرةً نحتاج إلى طريقة

البنية التي ستتعامل بالفعل

عملية الحصول على واجبات

لطبقة ثابتة ، لذا دعنا نفعل ذلك

سأقوم بكتابة مقبض الوظيفة

ثم

لا أريد بالفعل إنشاء

طريقة ثابتة ، أردت أن تكون هذه الطريقة

طريقة مثيل للبنية الخاصة بي ، لذلك سأقوم

بالمرور هنا حسنًا ، هذا

ما يعادل هذا في PHP حسنًا ،

لذا فإن المقبض سيعيد

عددًا صحيحًا من 32 بت تذكر أننا نحتاج إلى هذا

مرة أخرى إلى بيان المطابقة الخاص بنا إلى

رمز الخروج ، لذلك سيعيد هذا المقبض

عددًا صحيحًا من 32 بت وضمن هذا النطاق ،

نقوم بشكل أساسي بطباعة ما

كان لدينا من قبل في بيان المباراة الخاص بنا ، لذا

سأقوم بنسخ هذا الأمر

داخل طريقة المقبض الخاصة بي

وحفظ هذا فقط لإعادة السماح له

بالوضوح التام إذا مررنا الذات ضمن

الحجج التي تعني أن هذه الوظيفة

غير ثابتة ، هي طريقة مثيل

للبنية إذا لم أقم

بتمريرها ، تصبح هذه الطريقة ثابتة مشابهة

لـ PHP ،

لذا في هذه الحالة أريد أن يكون هذا

أسلوب مثيل لذلك سأقوم بتمرير

الذات

وأخيراً دعنا نستخدم بالفعل

فئة AKA الهيكلية و PHP ضمن بيان المطابقة الرئيسي الخاص بنا

حتى أتمكن من إزالة كل

هذا الشيء واكتب الأمر add

new ببساطة حتى أتمكن من استدعاء هذه الطريقة بشكل ثابت

لأنني لا أنجح أي

حجة ذاتية هنا ، ومع ذلك ، فإن طريقة المقبض

غير ثابتة ، وذلك لأنني أقوم بتمرير

الذات هنا ،

لذا عند الأمر بمقبض جديد ، ومن المفترض أن

يعود هذا إلى حد كبير ويخرج الكود ،

فلنحاول تجميع تطبيقنا

لنرى كيف يحدث

هذا. سأقوم بمسح شحنة تشغيل المحطة

ودعنا نستخدم

ملاحظات المحطة للمضي قدمًا في هذا الأمر ،

لذا

تخبرني الشحنة على اليمين أن

الأمر F لم يتم العثور عليه ضمن

نطاق الوظيفة الرئيسية ، مما يعني

أن الهيكل لم يكن كذلك تم استيرادها ، لذلك

دعونا نفعل ذلك بالطريقة نفسها التي نستخدمها

في PHP ، ننتقل فقط إلى

الاختلاف الأول في الباقي وهو أنني بحاجة إلى

إعلان أوامر الوحدة ، حسنًا ، لذلك على

main.rs الخاصة بي أحتاج إلى التصريح عن كل

ملف واحد موجود داخل دليل المصدر الخاص بي

ومن ثم يمكنني استيراد

الهيكل عن طريق استخدام أوامر

إضافة الأمر ،

لذلك قمت باستيراد دعنا نشغل البضائع مرة

أخرى واتبع التعليقات

الآن ، تخبرني البضائع أن

أمر الإعلان الذي قمت باستيراده للتو

داخل main.rs لدينا الملف عبارة عن

بنية خاصة ، شيء آخر رائع جدًا حول

الراحة هو أن الهياكل خاصة بشكل

افتراضي مما يعني أنه عند إنشاء

هذا الهيكل ، يكون الأمر خاصًا

بملف الأوامر ولا يمكنك استخدامه

في أي مكان آخر ما لم تستخدم هذه الكلمة الأساسية

Pub وقمت بإنشاء هذا الهيكل عام ،

سأقوم بمسح شحنة تشغيل المحطة

مرة أخرى ودعنا نرى كيف تسير الأمور

بعد ذلك ، تخبرني المحطة أنك

تتصل بالجديد والمقبض

ملتزم ، لكن كلاهما خاص ،

لذا فإن الوظائف المنفذة على البنى

هي أيضًا خاصة بشكل افتراضي مما يعني

أنني بحاجة إلى الوصول إلى هذه الوظيفة بشكل جديد

وجعل الوظيفة عامة بالفعل

حتى أتمكن من استخدامها خارج تشغيل شحن الملفات الخاص بي ،

يمكننا رؤية تعليمات العرض مما

يعني أنني متأكد تمامًا من أن

برنامجي يعمل كما هو متوقع لذلك إذا

بدأت في لعبة Cairo Run ، فقد رأيت إضافة

مهام وهي نتيجة وظيفة المقبض الخاصة بي ،

حيث يستمتع الجميع بها أو لا يستمتع بها ، حسنًا ،

هذا رائع ، دعنا نمضي

قدمًا هنا ، لذا نحتاج إلى فعل

الشيء نفسه إلى أمر list

لنفعل ذلك لأنه ليس لدينا الآن

أمر list حتى الآن مما يعني أن

الكود الذي يتم تنفيذه موجود ضمن

عبارة المطابقة هذه ، لذلك دعونا نفعل نفس

السلوك الذي فعلناه مع الأمر add ،

لذلك لدي هذا الرمز هنا.

اذهب إلى ما عليك سوى نسخه ولصقه مثل

الأيام الخوالي ،

وضع Bam هنا في الأسفل ثم

إعادة تسمية بعض الأشياء هنا بشكل واضح ، لذا

سيكون هذا هو أمر القائمة الذي يعيد أمر

قائمة جديدًا وأخيرًا

طريقة التعامل بدلاً من توجيهه للقيام بذلك

عرض

أو عرض

كل ما يجب القيام به على

ما يرام ، لذا نأمل في تشغيل البضائع

فعليًا ، فلنستخدم هذا الهيكل في

بيان المطابقة الخاص بي ، لذا سيكون هذا

أمر قائمة ، أعطني مثيلًا جديدًا لذلك

ثم تعامل مع المهمة ، لذا إذا قمت بتشغيل

البضائع ،

فلنرى ما يخبرنا به إنه يخبرني

بالطبع أن أمر list

غير واضح مما يعني أننا بحاجة إلى استيراد

هذا الهيكل داخل RS الرئيسي الآن

شيء واحد رائع وأتمنى أن أرى ذلك في PHP

أيضًا عندما ترى

أنك تستورد كل شيء من

Sim ملف ما يمكنك القيام به هو هذا

الشيء هنا ، وهذا الشيء يستورد

كل شيء من هذا الملف إنه نوع من

البطاقة البرية التي تستورد بشكل أساسي

وتتأكد من أن كل شيء في أوامر الملف هذا

قابل للعرض داخل main.rs

وظيفة السكر الاصطناعي الجميل مرة أخرى لذلك

هذا رائع جدًا ، سأقوم بمسح

شحنة تشغيل المحطة التي استطعت رؤيتها أنني

لم أفعل ذلك ، فأنا متأكد من

أن القائمة تعمل أيضًا كما هو

متوقع مرة أخرى عند تجميع الصدأ ما لم تكن قد

فعلت ذلك في اللغة الإنجليزية

وهو أمر صعب مع go copilot هذه

الأيام ،

مما يعني أن برنامجك

يعمل نوعًا ما كما هو متوقع ،

حسنًا ، دعنا نتحدث عن

الواجهات لأنني أعني بالنظر إلى

هذين الأمرين لدينا الأمر F

ولدينا أمر list

وهو شيء نوع من التشابه هنا هو

طريقة المقبض هذه ، مما يعني أن كلا

المقاطعتين تنفذان نفس

طريقة المقبض بنفس الطريقة التي تعني بدون

وسيطات وإعادة

كود الخروج مرة أخرى ، لذلك هناك شيء يمكننا القيام به هو أن نقول

حسنًا كل من أمر list والأمر F

وربما أخرى يحتاج الأمر في

المستقبل إلى تنفيذ

واجهة الأوامر على الأقل لجعلها جميعًا

تبدو متشابهة ، لذا دعنا نتحدث عن

الواجهات على Rust وسيكون هذا في الواقع

ممتعًا ،

لذا سأنتقل إلى وحدة command.rs الخاصة بي

وأنا سأعلن هنا في

الجزء العلوي من

واجهة الأوامر الخاصة بي

الآن ، هناك

اختلاف واحد على الرغم من الصدأ هو

أنك تعلن بالفعل عن واجهة مع

سمة الكلمة ، حسنًا ،

حسنًا ، حسنًا ، حسنًا ،

هكذا في PHP لديك هذا الشيء الذي يسمى

التجارة والذي يسمح لك بمشاركة

الطرق بين

لا يتأكد الأشخاص من الفصول الدراسية في الواقع ، مثل سبب

استمرار الأشخاص في استخدامها ، ولكن على

الصدأ من أجل الإعلان عن واجهة ،

استخدم سمة الكلمات الرئيسية ، لذا

فهذه هي في الواقع نفس الأشياء مثل

الكتابة هنا كما هو الحال في أمر واجهة PHP ،

حسنًا ،

الاختلاف هو هذا مشابه لـ PHP ،

يمكننا تحديد ما تفرضه هذه الواجهة

فعليًا على تعريف

طريقة التعامل التي تتلقى

المعنى الذاتي غير ثابت ثم تعيد

عددًا صحيحًا من 32 بتًا ، وهذا هو

تعريف الواجهة الخاص بي ، ولكن في حالة الراحة ،

يمكنك الحصول على تطبيقات افتراضية

مما يعني أنه يمكنك تقديم حسنًا ،

ستحصل البنية التي تنفذ هذه السمة على

هذه الطريقة التي تعمل بهذه الطريقة حتى

تتمكن من توفير تطبيق ملموس افتراضي لتقييد

تنفيذ هذه الواجهة في الوقت الحالي ،

سأقوم بعمل مماثل لـ PHP مما يعني

أنني لا أفعل تريد توفير

تطبيق افتراضي مما يعني أن الأمر متروك

للهيكل لتحديد

التنفيذ الملموس لطريقة المقبض ،

لذلك بمجرد أن يكون لدينا واجهة تعامل

وصدأ ، يمكننا نسخ الاسم

وربما تتوقع القيام بشيء

مثل

أوامر التنفيذ التي لا تفعل ذلك ' لنعمل بهذه الطريقة ، لذا

سأقوم بإزالته هنا

وسأنتقل فقط في الجزء السفلي من

الأمر "تنفيذ F" وسأقول إن

تنفيذ الأمر التالي

لأمر الإضافة هو التالي أدناه مباشرة لذلك

فهو ليس ضمن تعريف البنية ليس

ضمن أمر تنفيذ F الأول ،

إنه أقل من تنفيذ

الأمر للأمر f هو الأمر الموجود

داخل هذا القوسين

ومن ثم نحتاج إلى وضع جميع الطرق ،

حدده من خلال تلك الواجهة ضمن ذلك

تعريف النطاق لذلك سأقوم بنسخ

هذه الوظيفة ومعالجتها كلها سأقوم

بإزالتها هنا من الطرق

بدون أي واجهة وسأقوم

بنسخها أدناه مباشرةً ،

حسنًا

الآن شيء رائع واحد أرغب أيضًا في

ترى في PHP أنه عادةً عندما يكون لديك

واجهة ، فأنت نوعًا ما تحدد واجهة

برمجة التطبيقات العامة لفئة معينة ،

فلا أحد ينشئ واجهة مع

طرق خاصة ، لذا هنا ماذا في Rust إذا

كنت تحدد واجهة إدخال

أو طرق ملموسة لـ تكون الواجهة

لاستخراج

جميع الطرق عامة بشكل

افتراضي ، مما يعني أنه يمكنك إزالة

كلمة Pub الرئيسية هذه ،

وهي رائعة جدًا ، لذا نأمل أن لا

تزال الأشياء تعمل كما هو متوقع أو

لا ، لنرى تشغيل البضائع ، إنها

تشكو والآن تشكو

من البنية F لا يحتوي الأمر على

طريقة مقبض

ولماذا لأن

الواجهات أو السمات في Rust هي

في الواقع خاصة بشكل افتراضي مما يعني أنني

بحاجة إلى جعل هذه الواجهة عامة لاستخدامها

خارج نطاق

الأوامر ،

فلنقم بتشغيل البضائع مرة أخرى ونأمل أن

تعمل كما هو متوقع لا

يعمل كما هو متوقع ، هذا لأنني

بحاجة إلى حفظه ، وأعتقد أنه دعونا

نجعله ، دعنا نرى بالطبع

أحتاج إلى حفظه هناك ، إنه

يعمل الآن ويجمع ، وهو أمر

رائع يمكننا فعل الشيء نفسه

لأمر القائمة ، فلنفعل

لذلك هنا في أمر القائمة يمكنني

القيام به هنا مباشرة تنفيذ الأمر لأمر

القائمة ،

إنه الأمر الذي لدينا في معالجة الطريقة ، قم

بإزالته من هنا في الأعلى

وضعه داخل تعريف الواجهة ،

إزالة الكلمة الرئيسية pub مرة أخرى لا

من المنطقي أنها واجهة برمجة تطبيقات عامة ، فلنقم بتشغيل

البضائع وبالطبع إنها تعمل كما هو

متوقع وهو أمر رائع ، لقد

حصلنا على رؤية متغيرات وظائف واجهات

تبني أنواع البيانات كل هذا

الجنون ، فلنبدأ بالفعل في ترميز

الكود المسؤول للاستمرار في القيام بذلك

ثم عرض كل to-do's

وأردت أن أكون على دراية بأنك

تحتاج حقًا إلى القيام بالباقي ، فأنت

تحتاج فقط إلى معرفة مفاهيم

البضائع هذه ، وكيفية تشغيل البضائع ، وكيفية تثبيتها ،

ثم كيفية إنشاء هيكل ، وهذا

ليس الشيء الوحيد الذي عليك فعلاً

أن تفعل ما يرام عندما تحتاج إلى الوصول إلى

كود ملموس ومعقد

حقًا ، فسيقوم مساعد الطيار بعمل كل شيء من أجلك ، حسنًا ، لذا لا تحفظ

كل شيء أنا على وشك أن أعرضه لك جيدًا ،

لذا فإن الأمر نصف الموجود هنا

تريد أن تبدأ فعلاً في الاستمرار في

هذه المهمة ،

لذا فإن الاستمرار في القيام بأول شيء

يتعين علينا القيام به هو الحصول على وصف لذلك لفعل

الأشخاص المناسبين سيقومون بتشغيل

هذا الأمر بالطريقة التالية ،

إضافة ما أقوم به وصف واحدًا على سبيل

المثال ، لذا فأنت بحاجة إلى تقديم هذا

الوصف لفعل ذلك ، فإن

أول شيء يتعين علينا القيام به هو الحصول على

الحجج ويمكنني فعل ذلك بالكتابة ، دعنا نجادل

في الواقع لا ، أحتاج إلى تقديم

هذه الحجج في الحجج لأننا

نتذكر بالفعل الحجج

والوظيفة الرئيسية هنا ، لذا

نحتاج إلى توفير هذا الشيء

لهيكل أوامر AV والطريقة التي

سأفعل بها ذلك هي في الواقع من خلال إعلان

خاصية جديدة لهيكل

هذه الشاحنة سيكون لها خاصية

بالاسم الأقواس التي هي متجه للسلاسل

ثم على المُنشئ الخاص بي الذي هو

الجديد الذي سأقدمه

لهيكل هذه الحجج ، لذا سيكون هذا

متجهًا للسلاسل بشكل متساوٍ ، وأخيرًا

سأحتاج إلى تمرير هذه الحجة إلى

خاصية الهيكل لذلك سأفعل

شيئًا كهذا الآن مرة أخرى ، بعض

السكر الصناعي من الصدأ هو أنه إذا كان

اسم الخاصية مساويًا للحجة المعطاة ،

يمكنك ببساطة فعل ذلك Ben وهو

نفس الشيء تمامًا ، لذا فإن

امتلاك هذا أو هذا مكافئ

لذلك يمكنني فقط إزالة هذا وإعطاء

الأمر ADD الحجج ودعنا

نصحح

هذه الأقواس الذاتية بحيث تكون نفسها مرة أخرى

مكافئة لذلك ، فلنصحح هذه الحجج

لنرى ما إذا كان لدينا شيء ما في

المقبض ، فإن الطريقة التي

يشتكي منها الصدأ تخبرني

بعد إنشاء

مثيل جديد للأمر f ويتم ذلك على

وظيفة main.rs هنا ،

فأنت لا تقدم متجهًا

للسلاسل مما يعني أننا نفتقد وسيطة

الوسيطات ، لذا دعنا نفعل ذلك

هنا حيث نذهب ونجري البضائع

ولدينا الآن قائمة

الحجج داخل طريقة مقبض الأمر الخاصة بنا

مما يعني أنه يمكننا الحصول على

وصف لفعل ذلك

الوصف يمكنني أن أرى أنه إذا قمت بكتابة أحد المهام الخاصة بي ،

فسأتمكن من رؤية ذلك على رقم الفهرس

اثنان من Vector الخاص بي ، لذلك نحتاج إلى كتابة

شيء مثل هذا ،

دع الوصف

يساوي

الأقواس الذاتية ، احصل على رقم الفهرس الثاني الآن

تذكر أن الوصف قد يكون أو لا يكون

موجودًا لأنه إذا قدم المستخدم

إعلان تشغيل البضائع بدون الوصف ، فقد

لا يكون لدينا في الواقع رقم الفهرس الثاني

يعني أنه في هذه المرحلة ليس لدينا قيمة

فارغة ، فلدينا بدلاً من ذلك

خيار وصف يمكننا إلغاء تغليفه أو كما

رأينا في الوظيفة الرئيسية ،

ولكن في بعض هذه المرة سأفعل

شيئًا مختلفًا

عما أنا عليه سأفعل هذا فقط

إذا كان هناك

بعض

الوصف

وهذا قد يأتي من خيار الوصف ،

سأجعل هذا

أكبر قليلاً حتى يتمكن الجميع من المتابعة هناك

إذا كان لديك بعض الوصف قادم من

خيار الوصف الذي أريد تنفيذه

هذا الرمز هنا ومن أجل ذلك

سأقوم ببساطة بتصحيح الوصف

نفسه

ثم العودة مرة أخرى على الأرجح صفر

لأننا سنضيف المهام

وإلا أريد أن أقول للمستخدم

أن الوصف مطلوب

وأعيد الخروج مرة أخرى رمز الحالة

رقم واحد

حسنًا ، لذلك دعونا نلخص أننا نحصل على

الوصف الذي هو خيار قد يكون أو

لا يكون موجودًا في الفهرس رقم 2 ،

ثم إذا كان لدينا وصف ،

فسنقوم بإخراج الوصف أو

بالطبع إرساله إلى وصف المستخدم

مطلوب لذلك سأقوم بالركض وسأقوم

بمسح المحطة الطرفية الخاصة بي هنا ، مما يجعل

هذا أكبر قليلاً وسأقوم

بكتابة البضائع Run f

دون أي وصف على الإطلاق

ويخبرني أن الوصف مطلوب وسأصل

إلى انظر إلى هذا الشيء الأحمر الذي يعني أن

رمز الخروج كان واحدًا ، وإذا قمت بكتابة

تشغيل البضائع ، فقم بإضافته ،

فسأرى ذلك بالضبط على

المحطة الطرفية الخاصة بي ، مما يعني أنني أدخل

بيان if هذا هنا

الآن في هذه المرحلة ، لدينا وصف

وهي عبارة عن سلسلة ويمكننا تخزينها في

مكان ما على جهاز الكمبيوتر الخاص بي ، لذا فإن ما

سأفعله لمتابعة تنفيذ ساذج للغاية هو

أنه داخل الدليل الجذر الخاص بي ،

سأقوم بإنشاء ملف

باسم storage.txt

حسنًا ، هذا الملف

سنضع جميع المهام التي يجب أن يقوم بها الأشخاص هنا

يستخدمون أداة CLI الخاصة بنا ،

لذا ما يمكنني فعله هو استخدام وحدة نمطية

من الصدأ تسمى نظام الملفات ، لذا

سأقوم بالتمرير هنا في الأعلى. م

ذاهب لاستيراد وحدة من الصدأ من

المكتبة القياسية مرة أخرى شائع جدًا أن

جميع اللغات بها شيء من هذا القبيل

وسأقوم باستيراد وحدة نظام الملفات

من الصدأ ثم أقل مما

سأفعله ومرة ​​أخرى لن تحتاج

بحاجة إلى حفظ كل هذا ، اكتب

شيئًا مثل هذا ، دع

الملف القابل للتغيير يكون مساويًا في

خيارات فتح النظام السريع الجديدة وأريدك أن تفتح

هذا الملف

للكتابة في وضع الإلحاق

وأخيرًا اسم الملف هو الذي يحتوي

على اسم startage.txt

إذا خرج شيء ما عن القضبان

ولا ينبغي أن ينفجر عن القضبان ولكن إذا حدث شيء ما عن

القضبان ، فأنا أريدك أن تكتب ملفًا

غير موجود

جيدًا ، لذا في هذه المرحلة نفتح

هذا الملف للكتابة وما يمكننا فعله

هو ببساطة اكتب الآن

حسنًا في هذا الملف

محتويات وصفي

وهذا سيزيد بشكل أساسي كل

وصف للمهام التي يجب القيام بها في الأسفل ، لذا

دعنا ندير البضائع لنرى كيف تسير الأمور ،

أحتاج إلى علامة التعجب هنا ، ننتقل إلى

تشغيل البضائع ويضيف عدد المهام لقد رأيت

خطأً يقول إن

طريقة um غير موجودة في السطر الجديد ،

حسناً ،

أعطني ثانية

الآن ، ستطبع الطباعة في هذه الحالة ،

أريد حقًا أن أكتب على

الجهاز ، أنت تعرف ما سأذهب إليه افعل

شيئًا مختلفًا ،

يجب أن يعمل هذا ،

وإلا لدي النسخة الاحتياطية ،

حسنًا ، سأرى النسخة الاحتياطية الحقيقية

السريعة ، حسنًا

بالطبع حسنًا ،

ما سنفعله مرة أخرى ، نعم

بالطبع على وظيفة Ln الصحيحة ،

أحتاج بالفعل إلى المرور الملف كحجة

أولى ، لذا مرة أخرى في هذه الحالة ،

ما سأفعله هو أن أقول حسنًا اكتب

لي في هذا الملف محتويات هذا

الوصف هنا يمكنني تبسيط هذا

قليلاً من خلال القيام بذلك ، اكتب

محتويات هذا الوصف ولكن إذا

حدث شيء ما عن القضبان ، فيمكننا أن نتوقع أن

يكون الملف غير قابل للكتابة ، وبمجرد أن

أفعل ذلك ، يمكنني تقديم ملاحظات لمستخدمي ،

لذا أطبع

وسطرًا جديدًا ، لذا الآن على وحدة التحكم

لإضافته ،

فلنقم بتشغيل هذا على البضائع لنرى كيف

تسير الأمور مرة أخرى بدون أي شيء ، سوف

يشتكي ، إنه في الواقع لم يتم

تجميع التجميع وهذا لأنه

من أجل استخدام هذه الوظيفة

بالاسم ، اكتب Ln ، أحتاج بالفعل إلى استيراد

هذا في الأعلى ، لذا دعنا نفعل ذلك

مسح البضائع تشغيل المحطة مرة أخرى

دون وصف من المفترض أن

أدخل في الحالة "else" أدناه مباشرةً ،

وأنا كذلك مطلوب الوصف ،

ولكن إذا قمت بكتابة شحنة لأضيفها

ثم أفعل رقم واحد ، على سبيل المثال ،

يمكنني أن أرى ما سأضيفه وأنه

متأكد إلى حد كبير على حسابي. ليس هذا

هناك ، نذهب للقيام بالعدد الأول ، وإذا قمت

بذلك على سبيل المثال للقيام بالرقم الثاني ،

فسوف أرى ذلك أيضًا في ملفي الذي

أعتقد أنه رائع جدًا ،

حسنًا ، والآن بعد أن أصبحنا قادرين

على إضافة مهام إلى هذا الملف ، فإن

الخطوة الأخيرة هي بالفعل عرض جميع

المهام المطلوبة عندما نكتب قائمة تشغيل البضائع ،

لذا فإن الطريقة التي يمكننا القيام بها هي ببساطة عن طريق

تعديل رمز طريقة التعامل

داخل أمر القائمة ويمكن

القيام بذلك بسيط جدًا عن طريق القيام بشيء مثل

هذا ، دع المحتويات

تكون مساوية لنظام الملفات وقراءة

سلسلة المحتويات داخل

storage.txt ،

ولكن إذا حدث شيء ما ، فأنا

أريدك أن تكتب

ملفًا غير موجود

وبمجرد أن نحصل على المحتويات ، يمكننا استخدام

نفس اضغط باستمرار على

سطر جديد هذا هو ما يعادل الطباعة

في PHP ويمكنني أن أطبع

محتويات هذا الملف ، ما عليك سوى اتباع نهج

Nave لعرض جميع المهام ،

لذا مرة أخرى ، قم بإضافة تشغيل البضائع إلى

الرقم الجديد الثالث على سبيل المثال للقيام قم بتحريره بشكل

رائع ويمكنني الآن تشغيل قائمة تشغيل البضائع

للاطلاع على جميع المهام على الجهاز

الطرفي مرة أخرى ، وهو تطبيق أصلي للغاية

ولكن أنجز المهمة وقد

رأينا للتو كيفية إنشاء تطبيق CLI

على Rust والذي أعتقد أنه رائع جدًا ،

حسنًا ، آمل أن يكون الجميع متحمسًا

بشأن الصدأ على الأقل بالطريقة التي أريد

أن أوضح لك شيئًا آخر قبل أن

نغلق هذا ، أريد أن أريك

اختبارًا جيدًا

الآن للاختبار على Rust Works

مختلفًا قليلاً عما أنت نتوقع في

PHP أن نستورد أو نطلب

وحدة PHP أو ماضية ، ثم نحتاج إلى إنشاء

مجلد به اختبارات الاسم ثم

ملف منفصل لكل حالة اختبار ، لذا فهي

كتابة كثيرًا والباقي

مختلف قليلاً لذلك من أجل إجراء

اختبارات على سبيل المثال

لوحدة الأوامر ، فأنت تقوم بالفعل بكتابة تلك الاختبارات في

الجزء السفلي من الملف وهذا صحيح ، لذا

في نفس الملف الذي لديك

رمز المصدر الخاص بك أدناه ، ستخضع

لاختباراتك

لذا ما سنفعله يوجد هنا فقط

قسم منفصل لاختباراتنا ،

ثم سأقوم بكتابة

رمز وحدة الاختبار هذا ، لذا أخبرنا بشكل أساسي بالراحة ،

حسنًا ، كل الكود أدناه مرتبط

بالاختبارات ، لذا في اختبار الوحدة الخاصة بي ، أريد

استيراد كل شيء في هذا الملف

يمكن أن يتم ذلك باستخدام كلمة Super ثم

Wild Card

ثم لدينا هذا التعليق التوضيحي

لتحديد أنه ضمن هذا الشيء هو اختبار

الآن يمكننا إجراء اختبارين فقط

لعرض كيفية عمل الاختبار على Rust ،

لذا ما أريد القيام به هنا ببساطة قم

بإضافة أمر على سبيل المثال لإجراء اختبار

وتذكر الأمر add الذي نحتاجه

لتوفير إنشاء بنية جديدة باستخدام

الأمر name على سبيل المثال ، ثم

نحتاج إلى تمرير الوسيطات إلى هذا

الأمر Struct F لذلك سأذهب أولاً وقبل كل

شيء ، لإنشاء مصفوفة من الوسائط

التي ستكون متجهًا يحتوي على

الثنائي في الموضع الأول ،

تذكر ،

لذا سيكون هذا أمرًا يجب القيام به في السلسلة ، ثم

لدينا اسم الأمر وهو إضافة

إلى سلسلة أيضًا ،

ثم لدينا وصف المهام

الذي يشبه ما أقوم به من رقم

أربعة

إلى سلسلة

جيدة ، لذلك بمجرد أن يكون لدينا الحجج ، نعد

نوعًا من اختبارنا ،

يمكننا إنشاء بنية جديدة

للأمر F والتي ستكون أمرًا

يساوي إضافة أمر واستدعاء

الطريقة الثابتة للطريقة الجديدة والتي هي

البناء داخل هذا الجديد يمكنني تمرير

الحجج الخاصة بي وقد

أعددت اختباري بنجاح يمكنني الآن التصرف في الجزء

السفلي الذي يمكن إجراؤه ببساطة عن طريق

تخزين رمز الخروج

كنتيجة لمقبض الأمر بذلك

النوع من التمثيل ثم التأكيد بشكل صحيح ،

فما يمكنني فعله في الأسفل يسمى فقط

تأكيد القسم ويقول فقط

الآن أن هذا يشبه إلى حد ما PHP

على الرغم من أنني أستطيع كتابة تأكيد يساوي ثم

تقديم ما أحاول تأكيده

وهو الخروج الكود سيكون صفرًا ،

حسنًا ، فلنرى لنشغل هذا الشيء

لنرى كيف تسير الأمور

ويختلف أيضًا عن PHP هو أنك

تتوقع تشغيلها مثل وحدة PHP للمورد

أو عندما يتم تمريرها وفي

هذه الحالة تكتب فقط اختبار الشحن

وهو نوع من أنواع البضائع المضمنة ،

لذا إذا أجريت اختبار شحن ، فسترى الكثير من

الأشياء على المحطة مرة أخرى لتجنب

ذلك ، يمكنك ببساطة مسح قائمة انتظار

اختبار الشحن الطرفي ، تخفي قائمة انتظار Dash جميع

خطوات التجميع فقط لأنك لا تفعل ذلك أحتاج

إلى رؤيتهم

وأرى أن نتيجة الاختبار الخاصة بي على

ما يرام ، مما يعني أنني أحصل على رمز خروج 0

من طريقة المقبض الخاصة بي ، وإذا كنت على سبيل

المثال أجتاز واحدًا هنا ، فمن الواضح أن اختباري

سيفشل مع رمز الخروج رقم واحد ،

لذلك دعونا نجري هذه

الفئة q وأنا أحصل على هذه التعليقات على

وحدة التحكم الخاصة بي بأن القيمة اليسرى واحدة

ولكن القيمة اليمنى هي صفر مما يعني

أن اختباري يفشل

حتى أتمكن من وضع 0 لإجراء هذا الاختبار

ثم يمكنني القيام بالأشياء نفسها أدناه

أمر القائمة الذي

سأفعله حسنًا ، أنا فقط انسخ والصق

ثم اتصل بأمر القائمة هذا

لإعداد أمر القائمة ، ولست بحاجة إلى أي

حجج على الإطلاق ، لذا سأقوم فقط

بإنشاء حجة

بدون هذه الحجج أنا فقط أدرج

الأشياء ، ثم سأقوم باستدعاء

طريقة المقبض لاستعادة رمز الخروج الخاص بي

وإدخال رمز الخروج هو صفر ،

فلنقم بتشغيل اختبار الشحن Dash queue

وأرى أن كلا الاختبارين

يجتازان والاختبار النتيجة على ما يرام

وهذا إلى حد كبير ما كان علي أن

أعرضه عليكم

بضع ملاحظات صغيرة اليوم لقد

أظهرنا للتو تنفيذًا ساذجًا للغاية

للصدأ كتطبيق CLI بالطبع

حالات استخدام Rusty هي طريقة أكثر

تنوعًا من هذا هنا على سبيل المثال

لدي موقع الويب الخاص بي على مدونتي مكتوبة بالفعل

على Rust ، لذا إذا كنت تريد أن ترى

قليلاً كيف يبدو كود الصدأ في

الإنتاج ، فانتقل إلى GitHub

nunomodo.com الخاص بي وهو اسم المشروع

وستحصل على بعض الأشياء

المثيرة للاهتمام و الأشياء الحارة على Rust

مثل خيوط المعالجة المتعددة غير المتزامنة ، لذا فإن

الأشياء الرائعة والآمنة جدًا

على GitHub الخاص بي ، ستجد عددًا من

مشاريع الراحة بما في ذلك إطار عمل CLI

في الواقع ،

لذا فإن هذا المشروع معلق على gitsap.com

كان هذا صدأ

لمطوري PHP. لقد استمتعت بمشاهدتي وأنا أرتكب

الأخطاء وهو أمر رائع جدًا ، فلا

تتردد في متابعي على مواقع التواصل الاجتماعي الخاصة بي.

الكثير من

[تصفيق]

أي أسئلة أجنبية ،

لذلك كنت أتساءل مثل هل لديك

أي نصائح أو

أمثلة عن الأماكن التي يمكننا الذهاب إليها للتعلم

بمجرد أن نتقن ما أظهرته بالفعل ،

نعم ،

سأخبرك في الواقع الطريقة التي

تعلمت بها الصدأ الذي قد

يعمل من أجلك ، آمل أن تكون الطريقة التي

نجحت بها بالنسبة لي هي أنني بدأت أتعلم

من كتاب Rust وهو اسم

التوثيق الرسمي للصدأ

لأكون صادقًا حقًا يكتبون

كلمات بسيطة جدًا حتى للغة الإنجليزية غير الأم

مثلي وشعرت أنني

تعلمت ذلك حقًا من الكتاب الباقي ، ومن

ثم أيضًا أن شيئًا ساعدني

كثيرًا هو مع مساعد GitHub ، فأنت تحصل على

الكثير من الإكمال عندما تكون مثل

أه

أم عندما تواجه مشاكل ،

فأنت لا تعرف كيفية حلها ، فأنت في

الواقع تسأل مساعد كتاب الطيار أن

يكتب الكود لك حتى لو لم يكن

مثل الرمز الصحيح ، فأنت على الأقل

أنت. قادرون على التعلم من هناك ،

لذا فإن كتاب الملابس هو طريقتي إلى حد كبير

عندما تركض الصدأ ، ولكن

العب به واللعب مع

مساعد GitHub ، كما أنه سيساعدك في الأساس

على القضبان أشعر وكأنك

مرحب بك

واحدة سريعة جدًا ، لقد لاحظت أنك تمتلك نفسك

في لعبة Upper S وكأحرف صغيرة ، نعم ، هل

هي غير حساسة لحالة الأحرف

كلغة

أه لا يعني أنك

في الواقع سأحتاج إلى التحقق مرة أخرى من ذلك ولكن

في حالة الذات ، فعندما

تعود رجوع ، سأذهب

إلى هناك حقًا سريعًا ، لقد

كنت تتحدث بوضوح عن هذه

الذات هنا ، لذا عندما تعود إلى

هنا ، يمكنني فعل شيئين يمكنني القيام به على سبيل

المثال أمر القائمة فقط

تحديد البنية التي سأعود إليها

ولكنك يمكن أن تستخدم هذا السكر الاصطناعي بشكل أساسي

وتقول إنك تريد أن تعود

بنفسك وهذا يجب أن يكون كبيرًا

ولكن عندما تستخدم

الحجة الذاتية وهي أن هذا على PHP

يجب أن تكون حرفًا صغيرًا هنا ، حسنًا ،

البنية التحتية وراءها

الأطر و كل هذا النوع من الأشياء

كيف يمكن مقارنته بـ PHP

um هذا سؤال جيد حتى أتمكن من إخبارك

من التجربة مرة أخرى أنني لست

خبيرًا في Rust ولكن من التجربة أه من

أجل خدمة تطبيق PHP على

قطرات رقمية على سبيل المثال ،

سأحتاج إلى وضعها خلف ngx ولدينا

PHP fpm وأيضًا كل هذه

الأدوات المختلفة التي أستخدمها بشكل أساسي

كتكوينات افتراضية لأنني

لا أعرف كيفية تكوينها ولكن

في الباقي كان

علي ببساطة أن أقوم بالوكيل و Jinx إلى

خادمي على Rust وكان ذلك يعمل بشكل أساسي ،

لذلك كان لدي خادم بسيط للغاية على Ubuntu

يخدم

تطبيق الويب الخاص بتطبيق الصدأ ، وبعد ذلك

لدي فقط n Jinx يقوم بتوكيل جميع

الطلبات إلى هذا الخادم وهذا يعني أنه

لا يوجد مكافئ لـ PHP fpm بالنسبة لنا

جميعًا ، أعتقد أنني رأيتك

تغرد مرة أنك كنت

تبحث وتجرب

لغات أخرى للعثور على بعض المعرفات الجديدة لـ

PHP نعم وللحزم والأشياء الخاصة بك ،

نعم كنت أتساءل عما إذا كان هناك أي

أشياء بعض الأشياء التي وجدتها في Rush

أنت تريد تنفيذ نعم في الواقع ،

نعم ، سؤال ممتاز ممتاز

بالمناسبة ، لذلك هناك شيء قمت بالفعل بتحويله

إلى Laravel نفسه ، لذا في Laravel لدينا

رمز PHP حيث يمكنك فعلاً

قيم تفريغ VAR حتى تتمكن من استخدام شيء

مثل قيمة DD ومن ثم الحصول على وحدة التحكم

بإخراج قيمة السلسلة ، لذلك

هناك شيء لم يكن لدينا ويمكنك

العثور عليه على Rust ، لا أعرف ما إذا كنت قد

لاحظت ولكن عندما كنت أقوم بتنفيذ كود الصدأ الخاص بي ،

يمكنك رؤية هذا أم أه

أين تحصل عليه جيدًا لترى

بجانب قيمة التصحيح ،

أصل DD بشكل أساسي ، فأنت تعرف

ذلك عندما تستخدم على Rust شيئًا

مثل هذا ،

هذه الوظيفة مثل dbg ، ثم تضع

الأمر في الداخل في وضع الراحة ،

ترى قيمة الأمر وأيضًا

أصل قيمة

الملف الذي يأتي منه على PHP ،

لم يكن لدينا ذلك على الأقل في laravel DD ،

لذا فقد قمنا الآن بالفعل بنقل هذا

الشيء عند الغوص في بعض

قيمة التفريغ باستخدام دالة DD على PHP

يمكنك الاطلاع أيضًا على الملف الموجود على وحدة التحكم ،

لذلك هذا شيء تمكنت بالفعل من

نقل PHP وهو أمر رائع جدًا ، وكان

السؤال الأخير قبل ذلك هو

وقت الكيك ،

شكرًا لك ، لقد كان عرضًا جيدًا جدًا

لدي سؤال حول الإضافة الأمر

حيث تستخدم بيان الملف وكنت

أتساءل هل يمكنك استخدام إلغاء التغليف أو

غير ذلك كما تستخدم في main.rs

سؤال جيد نعم يمكنني استخدام إلغاء التغليف أو

غير ذلك في هذه الحالة أريد أن

أعرض قليلاً حول ما

إذا السكر الاصطناعي هنا ، وهو أمر جيد إذا كان

لديك شيء ما تنشئ متغيرًا

به ، ولهذا السبب قمت

بعرضه بهذه الطريقة ، لذا يمكنك دائمًا الحصول على

خيار عندما تستخدم

شيئًا قد يكون أو لا يكون فارغًا ، لذا

في هذه الحالة يمكن أن تفعل شيئًا

كهذا وهو إنشاء متغير من

شرط if ، ولكن يمكنك أيضًا

القيام بذلك كما ذكرت عدم التفاف أو ما

هو أشبه بـ "حسنًا" ولف

السلسلة ، وإلا قمت بتنفيذ هذا

الاستدعاء الافتراضي ، لذلك أردت عرض كليهما

لهذا السبب لقد فعلت ذلك سؤالًا جيدًا بشكل مختلف

وأشكركم على التصفيق مرة أخرى

[تصفيق]

all right can everyone hear me I guess

even in the background which is cool

welcome to rust for PHP Developers for

those who don't know me my name is Nuno

Maduro I am laravelcore team member and

I'm the creator of multiple open source

tools on the PHP ecosystem such as past

PHP Collision Lara stand openai for PHP

and more my mainstine project besides

working for a laravel of course is past

PHP

if you don't know what it is it's pretty

much an elegant PHP testing framework

with focus on Simplicity so if you are

curious about it go to passphp.com and

let me know what you think now today I

hope everyone is excited because they

are going to talk about rest from a PHP

developer is perspective and we are

actually going to do a full live session

of code but before we jump on that let

me give you a little bit of the history

of rust

most people think that rust was created

by Modzilla but in fact Russ was created

by a Mozilla employee with the name

Graden as a regular site project and

Graden was doing a lot of C in C plus at

the time and he said something like this

on the creation of the project

my main iron deals for rust are

frustrated C plus plus developers who

pick C plus plus and Duo its performance

but they like to have something safer

now if you work with systems languages

such as C or C plus you do face this

kind of stuff all the time segmentation

fault double free or corruption buffer

overflow this is pretty much what you

face all the time and Grading wasn't

happy about it

so what it does what it made is like he

made rust a memory safe language to

really fight other errors we just saw

before

but with the same advantages such as C

and C plus plus which is bare metal

performance now

Mozilla which was the company grading

was working at the time they actually

also saw the value on this they thought

we use a lot of rust and we use a lot of

C and C plus within Mozilla and we are

facing this kind of issues of memory

safety issues all the time in Firefox

for example so they said grading just

work on that full time within Mozilla

and let's see more rust within Mozilla

itself

and actually that happened during 10

years Russ is you're being heavily used

within Mozilla but in 2020 they got

affected by layoffs and the rust team

got this banned or canceled within um

Mozilla now in 2020 a lot of companies

were using already a lot of rust and

they created this thing called the rust

foundation and this wrist Foundation is

backed by very small companies as you

can imagine such as AWS Google meta and

Microsoft so very big companies with a

lot of money sponsoring the rust

foundation in really ensuring that rust

moves forward with it

and to give you a perspective how much

these companies are investing in Rust

this is a graph of the Android release a

product from Google and this graph

represents the memory safety

vulnerabilities the Android software

faces per year and we can see that in

2019 the the Android project faces more

than 200 memory safety vulnerabilities

in four years after which is the time

graph here on the bottom we can see that

the Android software hen has now less

than 100 memory safety vulnerabilities

so guess what change it during that four

years time cap well they're actually

starting to implement more rust within

the Android software and now they

actually have the same amount of rest in

C code within the Android product

foreign

even Google even said zero memory safety

vulnerabilities were discovered in

Android rust code so all the new code is

shipped on rest all of it had zero

memory safety vulnerabilities and also

predominant members on the JavaScript

Community which is Ryan the creator of

node in Dino both runtimes of the

JavaScript ecosystem he said I have the

most fun writing rust these days and I

will never start a c plus project again

so this is a heavily C plus plus

user who says I will never use C plus

plus again

now besides the memory safety that rust

brings into projects He also improves

the developer experience a lot

there is this thing called it the stock

overflow developer survey which is run

every year

and rest is for the seventh year in a

row the most limited programming

language in the world with eighty

percent of the wait more than 80 percent

of the developers who work with rust

want to continue to work with rest now

because this is a PHP conference I

thought that is funny to actually see

where is PHP in all of this

so

well

now to be really honest is a little bit

different because people who started to

work with rust they started immediately

with the modern language with all these

features that rust has and with PHP is

not fair so PHP have 40 percent of the

developers who work with PHP still want

to continue to work with PHP and this is

happening I think in my opinion it's

because not everyone is lucky enough to

work with modern PHP so we have composer

PHP Stan Peach Punit in past and not

everyone is lucky enough to work with

modern PHP there is still a lot of PHP

developers who work with Legacy projects

now that being said let's jump into some

code here so what we are going to do

today is that we are going to do a

simple CLA application on rest

that will be a to-do list on the CLI

where people can add a to-do and then

display all the to-do's okay so a very

simple regular rest application and I'm

gonna demo this showing you some

equivalents in PHP so everyone can get

familiar and get started with this

all right I'm going to jump here into my

terminal in my Sublime Text which is my

editor of choice then I have this to do

backup in case things goes off rails

okay

hopefully no

so the first thing we are going to do is

install rust on my machine just be

mindful that installing gross on my

machine works in the same way that

installing rust on Ubuntu or or

a Windows operating system and what you

have to do is really type this command

this script called addressed up okay and

I can download it with the curl for

example and run it and what rest up does

it's mainly installing two binaries on

my machine one is rust C and the other

one is Cargo okay so we are going to get

those two tools rust C and cargo once I

run this script

once it's run we get access to Cargo and

cargo to give you the equivalent on PHP

is pretty much the composer of rust with

this we can add dependencies remove the

dependencies and even start a new

project

so this is the first tool we get

after cargo we also get something called

address C and rust C is pretty much the

equivalent of the PHP binary

in in the sense that with resc you

actually compile code that you can run

afterwards and with PHP you interpret

and run code so there are the equivalent

part in different languages

now again with cargo you can add

dependencies remove dependencies result

or start a new project you can do the

similar thing with composers create

project so let's do that I'm going to

type cargo you need to do now one thing

to keep in mind here is that if I just

do it like this I'm going to create a

new binary application however if I pass

the option dash dash leave I'm going to

create a new package like a library

basically to others can use in this case

I'm going to remove the dash dash lip

and I'm going to Simply type cargo we

need to do in this as the feedback from

the terminal is telling me we are going

to we are creating a binary application

now let's take a look about the file

structure here so I'm going to open this

to do on my left and I'm going to open

this file with the name

cargo.tomel file

the cargo.com is the equivalent of

composer.json mean that we have all the

meta information about our application

in this case I obviously have my

application name which is to do the

version of my application which is 0.1

on the equivalent side on PHP we

actually tag use git tag to actually

know the the version of the package

however on Rust use this flag version

then we have the rust Edition the rest

Edition is the equivalent of the PHP

versions We have php5 php7 php8 however

with rust we work with editions okay and

this is the Edition required to run my

run my rust project

then on the bottom I have this list of

dependencies similar to composer

required dependencies where I can

specify which Community dependencies my

rest project is using I could come I

could come here and do something like

Chrono equals to

4.0.0 and this would simply mean that my

project uses the community date time

library of rust which is Chrono for my

CLI app I actually don't need any

dependencies so I'm going to keep this

list empty for now

now I want to show you the cargo.log

file which is another file and for that

I'm going to compile my project for the

first time and to do that I'm going to

Simply type cargo run

to see I need to enter on the folder

first

and then I'm going to type cargo run now

when I type cargo run the first thing I

see on top is the compilation steps of

my project so I can see that my project

got compiled and then finished compiling

and after is kind of executing the

binary the result of the compilation and

I can see Hello World on my console

which is exactly what my source code is

telling me to do

now once I run cargo for the first time

I can actually run cargurun Dash queue

which is my favorite way of running

cargo which basically hides all the

compilation steps I see here on top it

just prints exactly what my program is

printing out

so once we run cargo for the first time

I get this file which is cargo.lock and

this is pretty much the equivalent of

the composer.lock it's basically a

manifest of the dependencies use it my

my project

then I also have this target folder

which compiles contains all the

temporary files result of the

compilation I kind of want you to ignore

this folder for now just be mindful that

this target folder represents some of

the temporary files used on the process

of compilation

and then we have our source directory so

pre equivalent to PHP however within

this Source directory we have our

main.rs file the main.rs is the

equivalent of the public index.php which

is the entry point of your application

and I can see as well that within this

main.rs I have a function with the name

main which is equally the entry point of

my app and we can immediately start

understanding some stuff here about Russ

the first thing is Declaration of

functions I can declare functions with

the F and keyword then giving the

function name and then the list of

parameters now as you can see here the

type is not specified this is one cool

thing about rest is that the type

inference is very strong so here if I

wanted to be explicit I can say that the

return type of this function is void so

this is the equivalent of the PHP thing

like this basically so here this

function is returning void meaning

nothing and then when I have a macro

print OLN which is like a function that

displays Elder world and of course if I

run cargo here

I get to see Hello World on my console

so

again we are doing a CLI application so

probably the first thing we need to do

here is explain the usage to the user

that will be something like usage to do

which is the way I want to use my

application and then to do and then I

pass the command and finally a list of

the of this arguments

okay

so obviously if I run cargo I should see

this on the console which I do so what I

can do just below is type now the list

of commands

and the list of commands will be

basically something like this commands

equals to two commands by the way we are

working with two commands today one of

them is to basically people want to run

cargo run ad and then the to-do

description like to do one for example

and the other one we are going to build

together is the list to basically

display everything we have added to the

to-do list so let's let's explain that

to the user this will be the add command

which receives a description

as an argument and this command adds a

to-do

and then just below we have the list

command

which basically displays

all to do's

if I run this with cargo hopefully

things are still working as expected

which they are something I can do here

to make this a little bit more sexy

is give some space here okay so I'm

going to do that

oops cargo

run Dash queue there we go

and I get to see the usage right on my

terminal

now now that we know a little bit about

the functions are you looking at this

function main something we can do is

extract this usage to a new function

with the name display help so let's do

that display help returns void

and then the function body will be

pretty much a copy paste of the content

of my previous main function and here

I'm going to call display help to get to

see the usage in the same way which I do

now

once we have

the usage the the user actually will

interact with this with this CLI tool

doing something like cargo run and then

passing the command which can be add or

list so we need to actually capture the

arguments of this CLA application

now to catch a to catch the arguments we

can actually use something building on

Rust call it

um the aims from the standard Library I

want to talk a little bit about that so

in rest

actually I want to talk about variables

before that I'm sorry

so to catch the the we need to catch the

arguments and then the command but we

need to store those values somewhere and

we usually store that on variables

and let's talk a little bit about

variables on Rust before we actually

talk about the standard Library

um so variables on Rust are not actually

variables meaning that they don't change

the value they are immutable by default

this is a little bit different from PHP

as variables can change they are

actually variables so on rest when I

type something like I'm going to just do

this on a main

when I type something like let command

equals to

f

and then I just type below command

equals to list

on PHP this would work meaning that the

value of the command will change however

on rest once I try to debug what is

within command

you will see that rust will start to

complain so a couple of things here we

need to take a look of the first one is

that the rest compiler on the console is

telling me that you cannot assign twice

to an immutable variable meaning that I

have assigned the value add here but

then after I have tried to assign a

different value to the variable command

and that will not work in Rust so the

variables are immutable by default now

thanks to this great developer

experience on the console we can do we

get to see here we can see that Russ is

telling me that consider to making this

binding mutable which is which can be

done by the mute keyword meaning that if

I come here and I just type let mood key

command I now get to see on the terminal

the value

of my command which is list write that

on the bottom however rust is very

helpful again on the console it's really

just telling me though you're just

assigning like command to add and then

you are overriding just below doesn't

make any sense therefore I'm going to

tell you a warning here

however my program compiles and that's

thanks to the mood keyword which allows

me to mutate variables so again

variables are immutable by default on

rest

and now that we know how to actually

create variables we can start to collect

the arguments coming from the CLI and be

explicit about this

um this variable I'm going to say that

the arcs is a vector of strings

this is the equivalent the vector in

Rust is the equivalent of the array in

PHP however we can say what it what is

within the vector which is

um values which are strings

So within the arcs are a vector of

strings and this Vector of strings will

be something from the standard Library

called it standard library and

and then

give me the arcs from the end of

standard Library

so I want to talk a little bit about

this the standard librarian rust is

pretty much some common helpers that are

commonly used within rust projects even

on every language actually we have this

is pretty much

helpers for people building applications

on PHP we have like the global and the

global server variable we have a bunch

of array functions we have a date time

Library as well which pretty much allows

you to build PHP applications with those

helpers okay in rest we have this

standard Library which gives me access

to specific modules one of them is the

amp module which we can grab the

arguments from this so obviously if I

try to

debug this so I'm going to do that

hopefully I have all the arguments on my

console

I have a mistake here I can import this

this is like this

what

Vector of strings what

had to do this

what the what the hell is happening

give me a sec that's why I've created to

do huh

oh I need to call it collect of course

I'm sorry about this guys

that's why you have the backup huh just

in case

all right now it's compiling and on the

bottom I have on the line eight my

Vector of strings which is displaying by

binary however I can add the command and

now I have displayed on my terminal the

add command on bottom I can pass a

description for it for example and I get

to see that description or on my Vector

of strings

so as you can see we can use this

functional rest dbg which is pretty much

the equivalent of a yfr dump in PHP and

we can see the values that are compiled

in runtime and we can see these values

on the terminal so once we get the

arguments remember we are trying to

capture the command so when I type A

Cargo run at we need to capture this

value to then do something with the

command as you can imagine if we have if

we have the add command we need to do

some code if we have the list we need to

do something else which is displaying

all the to-do's and after if we have

something that doesn't make any sense we

need to tell to the user that the

command is unknown so let's do that

let's capture the command by doing

something like let command equals to

args get the index number one because

remember the command is within the index

number one and one thing called in Rust

which you probably would expect to see

here is that in PHP we have this thing

like ESET if the array key exists within

the array then we capture the value well

on rest there is no new levels okay so

here I need to tell to rest the

following okay unwrap the string within

the key number one or else which is

otherwise we need to execute this

default callback so I'm going to do that

I'm going to say okay if there is

anything on the position one which is

the case right here however it's not the

case when I don't run any command I want

you to get me back that string however

otherwise you execute this default

callback and when the user doesn't

provide the command what we need to do

is display the help just like that and

then we need to exit the program with

feedback with the exit code I'm going to

do that using the process module from

rust again an Helper and I'm going to

exit with the static code with the

status code 0 and I need to import this

module right here on top so standard

Library process

okay so let's see how this is going

let's debug here as well the command to

see if we can capture the F or the list

so I'm going to Cargo run Dash queue and

I get to see the help meaning that we

are entering on this default callback

the key number one does not exist

however if I run cargo run Dash Q F I

get to see the ad on the console and I

get to see a list now one cool thing and

you probably get the same feeling with

PHP stand at level Max is that if the

program is compiling meaning it's kind

of working as expected that's a very

cool feeling with working with compiler

languages such as rest is the feeling

that if it's compiling it's kind of

working unless you make a typo somewhere

which is a very good feeling when

working with rust

now again at this point on the command

variable we have the add value we have

the list value however we also have

something that if the user types like

which is nothing at all we have the

command with this value which doesn't

actually represent anything we don't

know what to do with it so we need some

control flow to do something when it's

add to do something when it's list and

to do something when the value doesn't

make any sense and for that we can use

the match expression

the rest have pretty much the same stuff

as PHP so we have if conditions health

conditions we have the match statement

which is exactly what we are going to

use and the match can be done with

something like this okay

let the exit code be the result of the

following match expression if the

command a string

is equal to the value add

I wanted to do something within this

this block of code

if the command is list I want you to do

something else

however if the command is something that

I don't know meaning I want to enter on

this default scope I want to do

something else

and finally now that we have an integer

of 32 bits on the exit code what we can

do here is simply exit with that exit

code

and of course we need to get an exit

code from this massive match expression

so we need to type some code here

actually so I'm going to type print new

line with adding a to-do

and return back the integer of 32 bits

  1. now one very cool thing on rest is

that if you have something like this

meaning that you have a return plus a

semicolon that is the synthetic sugar

which is you remove the semicolon and

then you can remove the keyword

which is pretty cool so some synthetic

sugar so if you have this this is the

equivalent of having

this so you just hide the semicolon hide

the return statement which is in my

opinion pretty cool I used to see more

of these things in PHP to be honest

then we can do the same stuff on the

list command

instead of adding it to do we are

obviously displaying all to do's and

returning zero at the same time and

finally of course so if we enter on a

default statement meaning that we don't

have ADD Or List we need to say to the

user

unknown command

and you need to back off with the exit

status 1 which is an error so let's see

how my program is doing I'm going to

type

cargoon-q displays the help makes sense

Dash Q add which having a to do and then

list to display all to do's and then

and I get to see an unknown command so

so far it's working as expected except

doing the proper job of storing to-do's

and displaying all the tattoos but uh I

think it's working right

before we actually talk a little bit

about the concrete code that we'll store

those to-do's in a persistent layer

let's actually refactor these a little

bit because at this point my main file

have 42 lines and for the same reason

you don't store your code on the

index.php you probably organize your

code on PHP and within folders within

classes in rest is the same stuff we

basically refactor all our code when we

have to so looking at this at this

application on PHP how would I tackle

this problem I would probably use

Symphony console for this and I would

probably have a separate class with the

name add command to actually handle the

process of the storing it to do and then

I would add a list command to actually

display all the to-do's on my terminal

so let's do that on rust or at least try

to do the same thing so what I'm going

to do here

is that on my source directory I'm going

to create a new file that file will have

the name

commands.rs function

on this file I'm going to store the code

of my ad command and I'm going to start

just below the code of my list command

again

we don't have classes on the rest but we

have something that they say it's not

similar but it's kind of it is so we can

do kind of the same thing with something

called it a struct

so here I'm gonna I'm gonna create

something called it strict F command

and within this struct I can have

properties similar to classes on PHP so

here I could have something like name

equals to a string and then I could have

for example age equals to an integer of

32 bits there's a lot of dotty Types on

Rust like integer of 32 bits unsigned

integers which are positive integers

however today we are going to not talk

about that any too much

so I want to my ad command did not have

any properties for now

however I need to have at least one

method to handle the process of storing

the to-do on the persistor layer so

let's add actually some methods to

destruct the way we can do that on rust

is not within the struct definition it's

just on a scope below so what I can do

here is something like implement

F command and then I'm going to put all

the methods within this Implement F

command scope and I'm going to do is

create first of all my Constructor The

Constructor convention on rust is using

this new function name so new function

returns a self which is the structure

basically it's pretty much the cell

phone PHP as well

and then within this self within this

function a new scope I'm going to return

the add command

without any properties however remember

remember this when you have a semicolon

we can remove the semicolon remove the

return keyword and make this code a

little bit more simpler

so this is the equivalent of a construct

in PHP and just below we need the method

for the struct which will actually

handle the process of having a to-do to

a persistent layer so let's do that I'm

going to type function handle

and then

I don't want to actually create a static

method I wanted this method to be an

instance method of my struct so I'm

going to pass here self okay this is the

equivalent of this in PHP okay

so the handle will return back an

integer of 32 bits remember we need this

back to our match statement to the exit

code so this handle will return an

integer of 32 bits and within this scope

we are basically printing out what we

had before within our match statement so

I'm going to copy this

had to within my handle method

and just save this so to re to let it

very clear if we pass the self within

the arguments meaning that this function

is non-static is an instance method of

the struct if I don't if I don't pass

self this method becomes static similar

to PHP

so in this case I want this to be an

instance method so I'm going to pass

self

and finally let's actually use this

struct AKA class and PHP within our main

match statement so I can remove all of

this thing and simply type add command

new so I can call this method statically

because I'm not passing any self

argument here however the handle method

is non-static that's because I'm passing

the self here

so at command new handle and that should

pretty much return back and exit code

let's try to compile our application to

see how this is uh doing

so I'm going to clear my terminal run

cargo and let's use the terminal

feedback to actually proceed with this

so

cargo is telling me on the right that

the F command was not found within the

scope of the main function so meaning

that the struct was not imported so

let's do that in the similar way we do

in PHP we just go to the top one

difference in the rest is that I need to

declare the module commands Okay so on

my main.rs I need to declare every

single file that is within my source

directory and then I can import the

struct by doing use commands

add command

so I've imported let's run cargo once

more and just follow the feedback

now cargo is telling me that the ad

command that you just have imported

within our main.rs file is a private

struct another very cool thing about

rest is that structs are private by

default meaning that when you create

this struct add command it's private to

the commands file and you cannot use it

elsewhere unless you use this keyword

Pub and you make this struct public

I'm going to clear my terminal run cargo

once again and let's see how it goes

next the terminal is telling me that you

are calling the new and the handle

committed however both are private

okay so functions implemented on structs

are also private by default meaning that

I need to come to this function new and

actually make the function public

to be able to use it outside of my file

cargo run we get to see the display help

meaning that I'm pretty sure that my

program is working as expected so if I

start Cairo Run f I get to see adding a

to do which is the result of my handle

function

people are everyone is enjoying or not

huh cool okay that's cool let's move

forward here so we need to do the same

thing to the to the list command

let's do that because right now we don't

have a list command yet meaning that the

code getting executed is within this

match statement so let's do the same

behavior we have done with the add

command so I have this code right here

I'm going to Simply copy paste it like a

good old days

Bam put here on the bottom and then

rename some stuff here obviously so this

will be the list command which returns a

new list command and finally the handle

method instead of heading it to do will

display

or displaying

all to do's

okay so hopefully let's run cargo

actually let's use this struct within my

match statement so this will be list

command give me a new instance of that

and then handle the job so if I run

cargo

let's see what it tells us it's telling

me of course that the list command is

undeclarative meaning we need to import

this struct within the main RS now one

cool thing and I wish to see that in PHP

as well is that when when you see that

you are importing everything from the

Sim file something you can do is this

thing right here bam and this Imports

everything from that file it's kind of a

wild card that basically Imports and

makes sure that everything on that file

commands is exposable within the main.rs

function nice synthetic sugar again so

this is pretty cool I'm going to clear

my terminal run cargo I get to see I

didn't get to do so I'm kind of sure

that the list is also working as

expected again when rust compiles unless

you have made like to do in English

which is difficult with go copilot these

days

um meaning that your program is kind of

working as expected

all right cool let's talk about

interfaces because I mean looking at

these two commands we have the F command

and we have the list command something

that is kind of similar here is this

handle method meaning that both

districts implement the same handle

method in the same way meaning without

arguments and returning back the exit

code so something we can do is just say

okay both the list command and the F

command and maybe other command in the

future needs to at least implement the

command interface to make it all of them

look similar so let's talk about

interfaces on Rust and this actually

will be fun

so I'm going to go to my commands.rs

module and I'm going to declare here on

top

my command interface

now

one difference though on rust is that

you actually declare interface with the

word trait

okay

all right come down huh

so on PHP you have this thing called it

trade which allows you to actually share

methods between between classes people

are not actually sure about it like why

people are still using it however on

rust in order to declare an interface

use the keyword trait okay so in this

this is actually the same stuff as

typing here like in PHP interface

command okay

the difference is that similar to PHP we

can specify what this interface is

actually forcing the definition of which

is a handle method which receives self

meaning non-static and then returns back

an integer of 32 bits this is my

interface definition however on rest you

can have default implementations

meaning that you can provide okay the

struct who implements this trait will

have this method that works this way so

you can provide a default concrete

implementation for restricts

implementing this interface for now I'm

going to do similar to PHP meaning that

I don't want to provide a default

implementation meaning that it's up to

the struct to define the concrete

implementation of the handle method

so once we have the interface trades and

rust we can copy the name and you

probably would expect to do something

like implements

command it doesn't work that way okay so

I'm going to remove it here and I'm

going to go just on the bottom of the

Implement F command I'm going to say the

following

the implementation of command for add

command is the following just below so

it's not within the struct definition is

not within this first Implement F

command it's just below implementation

of command for f command is the the one

within this uh this two brackets

and then we need to put all the methods

Define it by that interface within that

scope definition so I'm going to copy

this function handle all of it I'm going

to remove it here from the methods

without any interface and I'm going to

copy just below right here

okay

now one cool thing which I also wish to

see in PHP is that usually when you have

an interface you are kind of defining

the public API of a specific class

nobody creates an interface with private

methods right so here what in Rust if

you are defining an input an interface

or the concrete methods of an interface

to extract

all the methods are just public by

default so meaning that you can remove

this Pub keyword

okay which is pretty cool so hopefully

things are still working as expected or

not let's see cargo run oh it's

complaining and now it's complaining

about the struct F command does not have

a handle method

and why because

interfaces or traits in Rust are

actually private by default meaning I

need to make this interface public to be

used outside of the scope of the

commands

let's run cargo once more and hopefully

it's working as expected it's not

working as expected that's because I

need to save it I guess

let's make it let's let's see of course

I need to save it there you go it's now

running and compiling which is pretty

cool we can do the same thing for the

list command let's do that

so here on the list command I can just

do right here implementation of command

for list command

it's the one that we have on the method

handle remove it from here on the top

put it within the interface definition

removing the pub keyword again doesn't

make any sense it's public API let's run

cargo and of course it's working as

expected which is cool

okay we got to see variables functions

interfaces structs data types all this

craziness let's actually start coding

the code responsible to persist that to

do and then display all the to-do's and

I wanted to be mindful of you really

just need to to do the rest you really

just need to know these concepts of

cargo how to run cargo how to install it

and then how to create struct this is

not the only thing that you actually

need to do okay when you need to come to

really concrete and complicated code go

co-pilot will basically do everything

for you okay so do not memorize

everything I'm about to show you okay

so the half command which is right here

we want to actually start to persist in

this to-do's okay

so to persisted to do the first thing we

need to do is grab the description of

that to do right people are going to run

this command on the following way cargo

run add my to do description one for

example so you need to provide this

description of the to do

so the first thing we need to do is grab

the arguments and I can do that typing

let args actually no I need to provide

those arguments on the arguments because

remember we have already the arguments

and our main function right here so we

need to provide this thing to the AV

command struct and the way I'm going to

do that is actually by declaring a new

property of my struct

this truck will have a property with the

name arcs which is a vector of strings

and then on my Constructor which is the

new I'm going to actually give to my

struct those arguments so this will be

equally a vector of strings and finally

I need to pass this argument into the

property of my struct so I'm going to do

something like this now again some

synthetic sugar of rust is that if the

property name is equal to the given

argument you can simply do this Ben and

it's exactly the same thing okay so

having this or this is equivalent

so I can just remove this and give to my

ADD command the arguments and let's

debug

those self arcs so itself again is the

equivalent of this let's debug those

args to see if we have something on our

handle method

okay rust is complaining is telling me

the following when you create a new

instance of f command and that's done on

the main.rs function which is right here

you are not providing a vector of

strings meaning that we are missing the

arguments

argument so let's do that

provide here there we go and run cargo

and we now have the the list of

arguments within our Command handle

method meaning that we we can grab the

description of the to do so the

description I can see that if I type my

to do one

I get to see that on the index number

two of my Vector so we need to type

something like this

let description

equals to self

arcs get the index number two now

remember a description may or may not be

there because if the user provides cargo

run ad without the description we may

not actually have an index number two

meaning that at this point we don't have

null we have instead a description

option which we could unwrap or else as

we have seen on the main function

however some this time I'm going to do

something different

what I'm going to do is just say this

if

some

description

and that's may come from the description

option

I'm going to make this a little bit

bigger so everyone can follow there you

go

if you have some description coming from

the description option I want to execute

this code right here and for that I'm

going to Simply debug the description

itself

and then return back probably zero

because we are going to add the to-do

otherwise I want to say to the user

that description is required

and return back the exit status code

number one

okay so let's recap we are grabbing the

description which is an option may or

may not be there on the index number two

then if we have a description we are

going to Output the description or else

of course send to the user description

is required so I'm going to run I'm

going to clear my terminal here make

this a little bit bigger

and I'm going to type cargo Run f

without any description at all and it's

telling me description is required and I

get to see this red thing meaning that

the exit code was one and if I type

cargo run add to do

one I get to see exactly that on my

terminal meaning that I'm entering on

this if statement right here

now at this point we have a description

which is a string and we can store it

somewhere on my computer so what I'm

going to do to follow a very naive

implementation is that within my root

directory I'm going to create a file

with the name storage.txt

okay so within this file we are going to

put all the to-do's people are here are

using our CLI tool

so then what I can do is use a module

from rust called it file system so here

on the top I'm going to scroll here I'm

going to import a module from rust from

the standard Library again very common

all languages have something like this

and I'm going to import the file system

module from rust then just below what

I'm going to do is and again you need no

need to memorize all of this is type

something like this okay let the mutable

file to be equals to in fast system

open options new and I want you to open

that file for writing

writing on append mode

and finally the file name is the one

with the name startage.txt

if something goes off rails which it

shouldn't go off rails but if something

goes off rails I want you to type file

not found

okay so at this point we are opening

this file for writing and what we can do

is simply type now okay

right on that file

the contents of my description

and this will basically increment every

description of the to-do's on bottom so

let's run cargo to see how it goes I

need the exclamation point here there we

go

cargo run adds to do number one I get to

see an error saying that um

method not found right in the new line

okay

description

give me a sec

now print will print out in this case I

just really want to uh write on the

terminal you know what I'm going to do

something different

okay this should work

otherwise I have the backup huh

okay I'm going to see the backup real

quick huh

yeah of course okay

so what we are going to do again yeah of

course on the right Ln function I

actually need to pass the file as a

first argument so again on this case

what I'm going to do is say okay write

me on this file the contents of this

description in here I can simplify this

a little bit by doing this write the

contents of this description however if

something goes off rails we can expect

the file to not be writable okay once I

do this I can give feedback to my user

so printing

and a new line so now to the console

to do added

let's run this on cargo to see how it

goes so again without anything it will

complain oh it's actually still not

compiling compiling and this is because

in order to use this function with the

name write Ln I actually need to import

this on top so let's do that

clear my terminal run cargo again

without a description I'm supposed to

enter on the else condition just below

and I am so description is required

however if I type cargo to do add and

then to do number one for example

I get to see to do added and that it's

pretty much sure on my um not this one

there we go to do number one and if I do

this with for example to do number two I

get to see that as well on my file which

I think is pretty cool

okay uh so now that we are we are able

to add to-do's to this file the last

step is really displaying all the to

Do's when we are typing cargo run list

so the way we can do that is simply by

modifying the code of the handle method

within the list command and that can be

done very simple by doing something like

this let the contents

to be equals to file system read

tostring the contents within the

storage.txt

however if something goes off rails I

want you to type

file not found

and once we have the contents we can use

the very hold print

new line This is the equivalent of print

in in PHP and I can do print out the

contents of that file just simply have a

Nave approach of displaying all the

to-do's so again cargo run add to new

number three for example to do edit

pretty cool and I can now run cargo run

list to get to see all the to-do's on my

terminal again a very native

implementation but get the job done and

we just have saw how to create a CLI app

on Rust which I think it's pretty cool

all right I hope everyone is excited

about rust at least in the way I am I

want to show you one more thing before

we close this off I want to show you

testing okay

now testing on Rust Works a little bit

different from what you would expect in

PHP we have to import or require a PHP

unit or past and then we need to create

a folder with the name tests and then a

separate file for each test case so it's

a lot of writing and the rest is a

little bit different so in order to have

tests for example for the commands

module you actually write those tests on

the bottom of the file that's correct so

on the same file that you have your

source code just below you will have

your tests

so what we are going to do here is just

have a separate section for our tests

and then I'm going to type this test

module code so basically telling to rest

that okay all the code below is related

to tests so within my module test I want

to import everything on this file that

can be done with a super and then the

Wild Card word

and then we have this annotation to

specify that within this thing is a test

now we can have two tests just to

Showcase you how a testing works on Rust

so what I want to do here is simply do

add command for example to have a test

and remember for the add command we need

to provide create a new struct with the

name command for example and then we

need to pass the arguments to that

struct F command so I'm going to first

of all create an array of arguments

that will be a vector that contains

within the binary on the first position

remember

so this will be a to do to string then

we have the command name which is ADD

to string as well

and then we have the to-do description

which is something like my to do number

four

to string

okay so once we have the arguments we

are kind of preparing our test

we can create a new struct

of the F command that will be command

equals to add command and call the

method static method new which is the

construct within this new I can pass my

arguments and I have successfully

prepared my test I can now act just on

bottom that can be done by simply

storing the exit code

as a result of the command handle so

kind of acting to then assert right so

on bottom what I can do is just called

the section assert and just say

now this is kind of similar to PHP

though I can type assert equal and then

provide what I'm trying to assert which

is the exit code to be zero

okay so let's see let's run this thing

to see how it goes

and also different from PHP is that you

would expect to run like vendor bin PHP

unit or when they're being passed and in

this case you just type cargo test

which is a kind of built-in within cargo

so if I run cargo test you see a lot of

stuff on the terminal again to avoid

this you can simply clear the terminal

cargo test Dash queue just hides all the

compilation steps because you don't need

to see them

and I get to see that my test result is

okay meaning that I get an exit code 0

from my handle method and if I would for

example pass one here obviously my test

would fail with the exit code number one

so let's run that

category q and I get this feedback on my

console that the left value is one

however the right one is zero meaning

that my test is failing

so I can put 0 to make this test pass

and then just below I can do the same

stuff for the list command which I'm

going to do okay I'm just copy paste

and then call this list command to

prepare my list command I don't need any

arguments at all so I'm going to just

create an argument

without those arguments I'm just listing

the stuff then I'm going to call the

handle method get back my exit code and

insert the exit code is zero

let's run cargo test Dash queue and I

get to see that both of my tests are

passing and the test result is okay

and this is pretty much what I had to

show you

um

a couple of small notes today we just

have shown a very naive implementation

of rust as a CLI application of course

Rusty's use cases are way more diverse

diverse than this here for example I

have my website my blog actually written

on Rust so if you want to see a little

bit what rust code looks like in

production just go to my GitHub

nunomodo.com is the name of the project

and you get to see some of the

interesting and spicy stuff on Rust such

as asynchronous multi-threading so very

cool and safe stuff

on my GitHub you will find a couple of

rest projects including a CLI framework

actually

so this project is hold on gitsap.com

this was rust for PHP developers I hope

everyone had fun watching me coding and

making mistakes which is pretty cool

feel free to follow me on my socials I

have Twitter Macedon LinkedIn YouTube

and just uh if you have taken pictures

sometimes you taken putting in social

media I like to see them okay thank you

so much

foreign

[Applause]

any questions

so uh I was wondering like do you have

any tips or

um examples of where we can go to learn

once we've mastered what you've already

shown

um yes

um

actually I'm going to tell you the way

I've learned rust which my uh it might

work for you I hope so so the way it

worked for me is that I started to learn

by the Rust book which is the name of

the the official documentation of rust

to be really honest they write very

simple words even for a non-native

English like me and I felt that I really

learned it just from from the rest book

and then also that something helped me a

lot is with GitHub co-pilot you get so

much out of completion when you are like

uh

um when you are facing problems you

don't know how to solve them you just

actually ask kitab co-pilot will just

write the code for you even if it's not

like the very correct one you at least

you are you are able to learn from there

so yeah dressbook is pretty much my way

to go when you when running rust however

play with it and play with GitHub

co-pilot as well it will basically uh

get you on the rails I feel like

you're welcome

very quick one is I noticed you had self

in Upper game capital S and lowercase

yeah is it case insensitive as a

language

uh no meaning that you would well

actually I need to double check that but

in the case of the self so when you are

returning back I'm gonna actually go

there real quick

uh you were talking obviously about this

self right here so when you are returned

here I could do two things I could do

for example list command just basically

specifying the struct I'm returning on

but you can basically use this synthetic

Sugar and say that you want to return

self and this needs to be uppercase

however when you are using the self

argument which is that this on PHP you

need to be a lowercase here okay

is the the infrastructure behind it

Frameworks and all that sort of thing

how how does it compare to PHP

um that's a good question so I can tell

you from experience again I'm not an

expert on Rust but from experience uh in

order to serve

in order to serve a PHP application on

the digitalocean droplet for example I

would need to put it behind ngx and we

have PHP fpm and also all these

different tools that basically I kind of

use as default configurations because I

don't know how to configure them however

on rest

I simply had to proxy and Jinx to my

server on Rust and that was basically

working so I had that on Ubuntu a very

simple server serving my rust

application web application and then I

just have n Jinx proxying all the

requests to that server and that's it

so no PHP fpm equivalent for us

all right

I think I saw you tweet once that you

were looking and experimenting with

other languages to find some new IDs for

PHP yeah and for your packages and stuff

yeah I was wondering if there are any

things some things you found in Rush

that you want to implement yeah actually

yeah excellent excellent question by the

way so something I have actually ported

to laravel itself so in laravel we have

this

the PHP code where you can actually

VAR dump values so you can use something

like DD value and then have the console

outputting the the string value so

something we didn't have and you can

find it on Rust I don't know if you have

noticed but when I was executing my rust

code you get to see this um

uh where is it well you get to see next

to the debug value

the origin of the DD basically you know

so when you're using on Rust something

like this

um

this function like dbg and then you put

actually the command inside on rest you

see the value of the command and also

the origin of the of the value of the

file where it's coming from on PHP we

didn't have that at least on laravel DD

so now we actually have ported this

thing in when you dive our dump some

value with the DD function on PHP you

get to see also the file on the console

so that's something I actually was able

to Port the PHP which is pretty cool

all right the last question before was

cake time see

um thank you that was very good demo um

I have a question about the add command

where you use the file statement and I

was wondering could you use unwrap or

else like you use in main.rs good

question yes I could use the unwrap or

else however in that case I want to

showcase a little bit about the the the

if synthetic sugar here which is okay if

you have something create a variable

with it that's why I've kind of

showcased it this way so you always get

back an option when you are using

something that may or may not be null so

in that case you can do something like

this which is creating a variable out of

an if condition however you could also

do as you have mentioned unwrap or else

which is more like okay and wrap the

string otherwise execute me this default

callback so I wanted to Showcase both

that's why I've done it differently good

question

and thank you let's have another round

of applause

[Applause]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment