Rust Comments
🦀 Rust Comments
They are ignored by the compiler and do not affect program execution.
1. Single-Line Comments
Use // for single-line comments.
2. Multi-Line Comments
Use /* ... */ for multi-line comments.
⚠️ Multi-line comments can be nested in Rust (unlike C/C++).
Example:
3. Inline Comments
Used at the end of a line.
4. Documentation Comments (Very Important)
It supports documentation comments that generate docs using rustdoc.
▶️ Line Doc Comment (///)
Used to document items like functions, structs, enums.
▶️ Block Doc Comment (/** ... */)
5. Module / Crate Documentation (//!)
Used at the top of files or modules.
6. Commenting Out Code (Debugging)
7. Best Practices for Comments
✔ Explain why, not what
✔ Use doc comments for public APIs
✔ Keep comments short & meaningful
✔ Update comments when code changes
❌ Avoid obvious comments:
🔑 Summary
| Comment Type | Syntax |
|---|---|
| Single-line | // |
| Multi-line | /* */ |
| Doc (item) | /// |
| Doc (module) | //! |
