Inline Documentation
note
Rust มีการสนับสนุนการเขียน Inline Documentation หรือ Inline Doc อย่างมีประสิทธิภาพ
ซึ่งเราสามารถเขียน Inline Doc ได้ผ่านการใส่ Comment /// แบบนี้ในรูปแบบของ
Markdown syntax ได้
tip
- Rust สามารถตรวจสอบความถูกต้องของ
Inline Docรวมถึงสามารถเขียน Test ภายในInline Docได้อีกด้วย โดยสามารถตรวจสอบได้ผ่านการรันcargo testเราจึงสามารถมั่นใจได้ว่าInline Docที่เราเขียนนั้นถูกต้องและสามารถใช้ได้จริง - ด้วยเหตุนี้ Library ส่วนใหญ่ของ Rust จึงมี
Inline Docที่มีประสิทธิภาพ และเพิ่มความสะดวกในการใช้งานอย่างมาก
Example
เขียน Inline Doc ด้วย ///
fn main() { my_function(); } /// # My Function /// /// This function adds two numbers together /// /// ## Arguments /// /// * `a` - The first number to add /// * `b` - The second number to add /// /// ## Returns /// /// The sum of `a` and `b` /// /// ## Example /// /// ```rust /// fn main() { /// let result = my_function(2, 3); /// assert_eq!(result, 5); /// } /// ``` fn my_function(a: i32, b: i32) -> i32 { // TODO: Implement a + b }
ผลลัพธ์๋ที่ได้
