Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

التعليقات (Comments)

يسعى جميع المبرمجين لجعل code الخاص بهم سهل الفهم، ولكن في بعض الأحيان يكون هناك ما يبرر وجود شرح إضافي. في هذه الحالات، يترك المبرمجون تعليقات (comments) في شفرة المصدر (source code) الخاصة بهم والتي سيتجاهلها المُصرِّف (compiler) ولكن قد يجدها الأشخاص الذين يقرؤون source code مفيدة.

إليك comment بسيط:

#![allow(unused)]
fn main() {
// hello, world
}

في Rust، يبدأ أسلوب comment الاصطلاحي بشرطتين مائلتين، ويستمر comment حتى نهاية السطر. بالنسبة لـ comments التي تمتد إلى ما بعد سطر واحد، ستحتاج إلى تضمين // في كل سطر، مثل هذا:

#![allow(unused)]
fn main() {
// So we're doing something complicated here, long enough that we need
// multiple lines of comments to do it! Whew! Hopefully, this comment will
// explain what's going on.
}

يمكن أيضًا وضع comments في نهاية الأسطر التي تحتوي على code:

Filename: src/main.rs

fn main() {
    let lucky_number = 7; // I'm feeling lucky today
}

ولكن غالبًا ما ستراها مستخدمة بهذا التنسيق، مع comment على سطر منفصل فوق code الذي تشرحه:

Filename: src/main.rs

fn main() {
    // I'm feeling lucky today
    let lucky_number = 7;
}

لدى Rust أيضًا نوع آخر من comments، وهو تعليقات التوثيق (documentation comments)، والتي سنناقشها في قسم “نشر حزمة (Crate) إلى Crates.io” من الفصل 14.