49

rustdoc allows you to document struct fields and enum variants by including a doc comment above each line:

enum Choices {
  /// The first choice.
  First,
  /// The second choice.
  Second,
}

struct Person {
  /// The person's name.
  name: String,
  /// The person's age.
  age: u8,
}

These will show up with nice formatting in the HTML generated by rustdoc. However, I haven't seen any way of making similar nicely-formatted documents for function arguments. Is there an "official" way to document them or do you just have to describe them freeform in the function's main documentation section?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jimmy
  • 35,686
  • 13
  • 80
  • 98
  • FWIW, I prefer to leverage the type system. Instead of saying "This u8 must be a power of 2 or prime", make a `PowerOfTwoOrPrime` newtype with an appropriate constructor. – Shepmaster May 03 '15 at 02:51
  • 1
    No syntax for that nor are guidelines/conventions established. – bluss May 03 '15 at 08:08

3 Answers3

43

I've seen the following style used in some of the examples:

/// Brief.
///
/// Description.
/// 
/// * `foo` - Text about foo.
/// * `bar` - Text about bar.
fn function (foo: i32, bar: &str) {}

So far it's working fine for me too.

P.S. There's also an issue on this.
P.S. Check also the improved rustdoc linking and the search aliases in 1.48.
P.S. There's now a docu at https://doc.rust-lang.org/beta/rust-by-example/meta/doc.html

ArtemGr
  • 11,684
  • 3
  • 52
  • 85
26

According to the rust documentation, function docs are formatted like this:

#![crate_name = "doc"]

/// A human being is represented here
pub struct Person {
    /// A person must have a name, no matter how much Juliet may hate it
    name: String,
}

impl Person {
    /// Returns a person with the name given them
    ///
    /// # Arguments
    ///
    /// * `name` - A string slice that holds the name of the person
    ///
    /// # Examples
    ///
    /// ```
    /// // You can have rust code between fences inside the comments
    /// // If you pass --test to `rustdoc`, it will even test it for you!
    /// use doc::Person;
    /// let person = Person::new("name");
    /// ```
    pub fn new(name: &str) -> Person {
        Person {
            name: name.to_string(),
        }
    }

    /// Gives a friendly hello!
    ///
    /// Says "Hello, [name]" to the `Person` it is called on.
    pub fn hello(& self) {
        println!("Hello, {}!", self.name);
    }
}

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

    john.hello();
}

zingi
  • 1,141
  • 13
  • 23
22

Is there an "official" way to document them

There is not currently an official way to document arguments.

Steve Klabnik
  • 14,521
  • 4
  • 58
  • 99
  • 2
    Since this answer is 3 years old, I'd like to inquire if there are any updates about this - at least I couldn't find any. – Philipp Ludwig Mar 22 '18 at 10:57
  • 5
    There is not. Most people rely on the type and argument name, and it tends to be pretty clear. If clarifications are needed, most use a bit of prose, rather than something that rustdoc needs to understand. – Steve Klabnik Mar 22 '18 at 11:00