beginnerBasics
Documentation Comments
Write documentation with /// and //! comments.
Documentation Comments
Write documentation with /// and //! comments.
Difficulty
Beginner
Code
rust
/// Adds two numbers together.
///
/// # Examples
///
/// ```
/// let result = add(2, 3);
/// assert_eq!(result, 5);
/// ```
fn add(a: i32, b: i32) -> i32 {
a + b
}
/// A point in 2D space.
struct Point {
/// The x coordinate.
x: f64,
/// The y coordinate.
y: f64,
}
fn main() {
println!("2 + 3 = {}", add(2, 3));
let p = Point { x: 1.0, y: 2.0 };
println!("({}, {})", p.x, p.y);
}Explanation
Use /// for docs. cargo doc generates HTML.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the basics category to build a complete understanding of this topic.