Add a # Examples section in doc comments with a runnable code block to demonstrate function usage and verify correctness.
Write examples in doc comments using the # Examples section followed by a fenced code block that demonstrates usage and asserts the result.
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
x + 1
}
Examples in your code documentation show users exactly how to use your functions with real code. They act like a quick-start guide that also runs as a test to ensure your code works as described. Think of it as a recipe card attached to the function that proves the dish turns out correctly.