rust 如何记录函数参数?

gcuhipw9  于 2023-01-05  发布在  其他
关注(0)|答案(3)|浏览(206)

rustdoc允许你通过在每一行上面包含一个doc注解来记录结构域和枚举变量:

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

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

在rustdoc生成的HTML中,这些参数会以很好的格式显示出来。但是,我还没有看到任何为函数参数制作类似的格式很好的文档的方法。有没有一种“官方”的方式来记录它们,或者你只需要在函数的主文档部分以自由格式描述它们?

but5z9lq

but5z9lq1#

我在一些例子中看到了下面的风格:

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

到目前为止,它的工作很好,我也。
另外,这上面还有一个issue
P.S.检查1.48中改进的rustdoc * 链接 * 和 * 搜索别名 *。
P.S.现在https://doc.rust-lang.org/beta/rust-by-example/meta/doc.html上有一个文档

brjng4g3

brjng4g32#

是否有“正式”的方式来记录它们
目前还没有正式的方式来记录争论。

h7appiyu

h7appiyu3#

According to the rust documentation,函数文档格式如下:

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

相关问题