你如何记录函数参数?

Jim*_*dra 17 rust rustdoc

rustdoc允许您通过在每行上面包含doc注释来记录struct字段和枚举变体:

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

struct Person {
  /// The person's name.
  name: String,
  /// The person's age.
  age: u8,
}
Run Code Online (Sandbox Code Playgroud)

这些将在rustdoc生成的HTML中显示出良好的格式.但是,我还没有看到任何方法为函数参数制作类似的格式良好的文档.是否有"官方"方式来记录它们,或者您只需要在函数的主要文档部分中描述它们是否自由形式?

zin*_*ngi 32

根据 rust 文档,函数文档的格式如下:

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

Run Code Online (Sandbox Code Playgroud)


Art*_*mGr 16

我见过一些例子中使用的以下样式:

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

到目前为止,它对我来说也很好.


Ste*_*nik 13

是否有"官方"方式来记录它们

目前没有官方的方式来记录论点.

  • 由于此答案已有 3 年历史,因此我想询问是否有任何更新 - 至少我找不到任何更新。 (2认同)
  • 那没有。大多数人都依赖于类型和参数名称,而且它往往很清楚。如果需要澄清,大多数人会使用一些散文,而不是rustdoc需要理解的内容。 (2认同)