How to document Rust function arguments?
Asked Answered
R

3

58

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?

Rata answered 3/5, 2015 at 2:34 Comment(2)
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.Coextend
No syntax for that nor are guidelines/conventions established.Babin
H
40

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();
}

Hadj answered 30/8, 2021 at 11:4 Comment(0)
S
53

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

Simeon answered 3/5, 2015 at 11:1 Comment(3)
Sort of like JSDoc. I say it's more ergonomic too. You can see the full function documentation at a single glance.Barham
updated for the P.S. linking to the issue.Bleeder
Just want to points out other official reference: doc.rust-lang.org/beta/rust-by-example/meta/doc.htmlHyacinth
H
40

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();
}

Hadj answered 30/8, 2021 at 11:4 Comment(0)
R
23

Is there an "official" way to document them

There is not currently an official way to document arguments.

Rubella answered 3/5, 2015 at 22:47 Comment(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.Sardonic
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.Rubella

© 2022 - 2024 — McMap. All rights reserved.