首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >rust文档注释中的额外空格被解释为文档测试

rust文档注释中的额外空格被解释为文档测试
EN

Stack Overflow用户
提问于 2021-06-23 23:24:05
回答 1查看 35关注 0票数 0

似乎如果我在doc注释中留下一个空行,然后缩进4个空格,那么cargo会将其解释为doc测试,并在我运行cargo测试时给我一个失败。关于解释这一点的文档注释和测试,我应该知道什么?

下面是一些用lib.rs编写的示例代码:

代码语言:javascript
运行
复制
/// This is a great function.
/// y: a very dangerous option.
///
///     note: Use None for y or you'll be sorry.
pub fn thing(x: i32, y: Option<i32>) -> i32 {
    if y.is_some() {
        println!("explode!");
    }
    x
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_thing() {
        assert_eq!(thing(5, None), 5);
    }
}

如果我运行cargo测试,下面是输出:

代码语言:javascript
运行
复制
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests (target/debug/deps/test_docs-28c699ad5df73c48)

running 1 test
test tests::test_thing ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests test_docs

running 1 test
test src/lib.rs - thing (line 5) ... FAILED

failures:

---- src/lib.rs - thing (line 5) stdout ----
error: expected one of `!`, `(`, `.`, `::`, `;`, `<`, `?`, or `}`, found `None`
 --> src/lib.rs:6:11
  |
3 | note: Use None for y or you'll be sorry.
  |     -     ^^^^ expected one of 8 possible tokens
  |     |
  |     tried to parse a type due to this

error: aborting due to previous error

Couldn't compile the test.

failures:
    src/lib.rs - thing (line 5)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.03s

error: test failed, to rerun pass '--doc'

如果我从文档注释中'note‘之前的缩进中删除一个空格,那么一切都会按预期进行:

代码语言:javascript
运行
复制
/// This is a great function.
/// y: a very dangerous option.
///
///    note: Use None for y or you'll be sorry.
pub fn thing(x: i32, y: Option<i32>) -> i32 {
    if y.is_some() {
        println!("explode!");
    }
    x
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_thing() {
        assert_eq!(thing(5, None), 5);
    }
}

输出:

代码语言:javascript
运行
复制
Compiling test_docs v0.1.0 (/Users/cleverpiggy/test_docs)
    Finished test [unoptimized + debuginfo] target(s) in 0.34s
     Running unittests (target/debug/deps/test_docs-28c699ad5df73c48)

running 1 test
test tests::test_thing ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests test_docs

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

这到底是怎么回事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-23 23:26:35

用4个空格缩进相当于用反引号括起来-有关更多信息,请访问the syntax reference

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68102753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档