Rust 为我们提供一个非常强大的特性:可以在文档注释中实现代码跳转。 将以下代码添加到 src/lib.rs : // in lib.rs /// Add one to the given value and return a [Option] type pub fn add_three(x: i32) -> Option { Some(x + 3) } 除了跳转到标准库中,我们还能跳转到项目中的其它模块。 // in lib.rs mod a { /// Add four to the given value and return a [Option] type /// [crate::MySpecialFormatter] pub fn add_four(x: i32) -> Option { Some(x + 4) } } struct MySpecialFormatter; 文档属性 下面是很常用的 #[doc] 属性,该属性可以被 rustdoc 所使用。 inline 可以用于内联文档, 而不是链接到一个单独的页面。 #[doc(inline)] pub use bar::Bar; /// bar docs mod bar { /// the docs for Bar pub struct Bar; } no_inline 用于防止链接到单独的页面或其它地方。 // Example from libcore/prelude #[doc(no_inline)] pub use crate::mem::drop; hidden 通过这个属性让 rustdoc 不要将下面的项包含在文档中: // Example from the futures-rs library #[doc(hidden)] pub use self::async_await::; 对文档来说, rustdoc 被社区广泛采用,大家所看到的标准库文档也是基于此生成的。 完整的代码 doc-comments 的完整代码可以在这里找到 格式化输出 位置参数 1. / 填 空 / fn main() { println!(“{0}, this is {1}. {1}, this is {0}”, “Alice”, “Bob”);// => Alice, this is B assert_eq!(format!(“{1}{0}”, 1, 2), ); assert_eq!(format!(, 1, 2), “2112”); println!(“Success!”); } 具名参数 2. fn main() { println!(“{argument}”, argument = “test”); // => “test” / 填 空 / assert_eq!(format!(“{name}{}”, 1, ), “21”); assert_eq!(format!(,a = “a”, b = ‘b’, c = 3 ), “a 3 b”); / 修 复 错 误 */ // 具 名 参 数 必 须 放 在 其 它 参 数 后 面 println!(“{abc} {1}”, abc = “def”, 2); println!(“Success!”) }

好文推荐

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。