Documentation

Doc comments are very useful for big projects that require documentation. When
running Rustdoc, these are the comments that get compiled into
documentation. They are denoted by a ///, and support Markdown.

  1. #![crate_name = "doc"]
  2. /// A human being is represented here
  3. pub struct Person {
  4. /// A person must have a name, no matter how much Juliet may hate it
  5. name: String,
  6. }
  7. impl Person {
  8. /// Returns a person with the name given them
  9. ///
  10. /// # Arguments
  11. ///
  12. /// * `name` - A string slice that holds the name of the person
  13. ///
  14. /// # Example
  15. ///
  16. ///
  1. /// // You can have rust code between fences inside the comments
  2. /// // If you pass --test to Rustdoc, it will even test it for you!
  3. /// use doc::Person;
  4. /// let person = Person::new("name");
  5. /// ```
  6. pub fn new(name: &str) -> Person {
  7. Person {
  8. name: name.to_string(),
  9. }
  10. }
  11. /// Gives a friendly hello!
  12. ///
  13. /// Says "Hello, [name]" to the `Person` it is called on.
  14. pub fn hello(& self) {
  15. println!("Hello, {}!", self.name);
  16. }

}

fn main() {
let john = Person::new(“John”);

  1. john.hello();

}

  1. To run the tests, first build the code as a library, then tell rustdoc where
  2. to find the library so it can link it into each doctest program:
  3. ```bash
  4. $ rustc doc.rs --crate-type lib
  5. $ rustdoc --test --extern doc="libdoc.rlib" doc.rs

(When you run cargo test on a library crate, Cargo will automatically
generate and run the correct rustc and rustdoc commands.)