这些是我的src/lib.rs文件的内容:
pub fn foo() {}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
foo();
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行时cargo test,出现以下错误:
pub fn foo() {}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
foo();
}
}
Run Code Online (Sandbox Code Playgroud)
如何foo从test模块内部调用?
ken*_*ytm 15
您可以使用super::来引用父模块:
fn it_works() {
super::foo();
}
Run Code Online (Sandbox Code Playgroud)
在 Rust 2015 中,您可以使用::来引用 crate 的根模块:
fn it_works() {
::foo();
}
Run Code Online (Sandbox Code Playgroud)
在 Rust 2018 中,可以使用crate::来引用 crate 的根模块:
fn it_works() {
crate::foo();
}
Run Code Online (Sandbox Code Playgroud)
或者,由于foo可能会重复使用,您可以use在模块中使用它:
mod tests {
use foo; // <-- import the `foo` from the root module
// or
use super::foo; // <-- import the `foo` from the parent module
fn it_works() {
foo();
}
}
Run Code Online (Sandbox Code Playgroud)
对于测试模块,从父模块导入所有内容也很常见:
mod tests {
use super::*; // <-- import everything from the parent module
fn it_works() {
foo();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3353 次 |
| 最近记录: |