在单独的模块中使用时使用未声明的类型或模块 `std` [英] Use of undeclared type or module `std` when used in a separate module

查看:56
本文介绍了在单独的模块中使用时使用未声明的类型或模块 `std`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

pub mod a {
    #[test]
    pub fn test() {
        println!("{:?}", std::fs::remove_file("Somefilehere"));
    }
}

编译时出错:

error[E0433]: failed to resolve. Use of undeclared type or module `std`
 --> src/main.rs:4:24
  |
4 |         println!("{}", std::fs::remove_file("Somefilehere"));
  |                        ^^^ Use of undeclared type or module `std`

但是,删除内部模块并自行编译它包含的代码可以正常工作:

However, removing the inner module and compiling the code it contains by itself works fine:

#[test]
pub fn test() {
    println!("{:?}", std::fs::remove_file("Somefilehere"));
}

我在这里错过了什么?如果模块位于单独的文件中,我会收到相同的错误:

What am I missing here? I get the same errors if the module is in a separate file:

ma​​in.rs

pub mod a;

a.rs

#[test]
pub fn test() {
    println!("{:?}", std::fs::remove_file("Somefilehere"));
}

推荐答案

默认情况下,编译器会在 crate root 的开头插入 extern crate std;(crate root 就是你传递给 rustc).此语句的作用是将名称 std 添加到 crate 的根命名空间,并将其与包含 std crate 的公共内容的模块相关联.

By default, the compiler inserts extern crate std; at the beginning of the crate root (the crate root is the file that you pass to rustc). This statement has the effect of adding the name std to the crate's root namespace and associating it with a module that contains the public contents of the std crate.

但是,在子模块中,std 不会自动添加到模块的命名空间中.这就是为什么编译器无法解析模块中的 std(或任何以 std:: 开头的内容).

However, in child modules, std is not automatically added in the module's namespace. This is why the compiler cannot resolve std (or anything that starts with std::) in a module.

有很多方法可以解决这个问题.首先,您可以在模块中添加 use std; 以使名称 std 在该模块中引用根 std.请注意,在 use 语句中,路径被视为绝对路径(或相对于 crate 的根命名空间"),而在其他任何地方,路径都被视为相对于当前命名空间(无论是模块,函数等).

There are many ways to fix this. First, you can add use std; in a module to make the name std refer, within that module, to the root std. Note that in use statements, the path is treated as absolute (or "relative to the crate's root namespace"), whereas everywhere else, paths are treated as relative to the current namespace (be it a module, a function, etc.).

pub mod a {
    use std;

    #[test]
    pub fn test() {
        println!("{:?}", std::fs::remove_file("Somefilehere"));
    }
}

您还可以使用 use 语句导入更具体的项目.例如,您可以编写 use std::fs::remove_file;.这使您不必键入 remove_file 的整个路径,而只需在该模块中直接使用名称 remove_file:

You can also use a use statement to import more specific items. For example, you can write use std::fs::remove_file;. This lets you avoid having to type the whole path to remove_file and just use the name remove_file directly within that module:

pub mod a {
    use std::fs::remove_file;

    #[test]
    pub fn test() {
        println!("{:?}", remove_file("Somefilehere")));
    }
}

最后,您可以通过在路径前加上 :: 前缀来避免完全使用 use 来要求编译器从 crate 的根命名空间解析路径(即转换路径进入绝对路径).

Finally, you can avoid using use altogether by prefixing the path with :: to ask the compiler to resolve the path from the crate's root namespace (i.e. turning the path into an absolute path).

pub mod a {
    #[test]
    pub fn test() {
        println!("{:?}", ::std::fs::remove_file("Somefilehere"));
    }
}

推荐的做法是直接导入类型(结构体、枚举等)(例如使用std::rc::Rc;,然后使用路径Rc),但要通过其父模块的导入来使用函数(例如 use std::io::fs;,然后使用路径 fs::remove_file).

The recommended practice is to import types (structs, enums, etc.) directly (e.g. use std::rc::Rc;, then use the path Rc), but to use functions through an import of their parent module (e.g. use std::io::fs;, then use the path fs::remove_file).

pub mod a {
    use std::fs;

    #[test]
    pub fn test() {
        println!("{:?}", fs::remove_file("Somefilehere"));
    }
}

旁注:您还可以在路径的开头写入 self:: 以使其相对于当前模块.这在 use 语句中更常用,因为其他路径已经是相对的(尽管它们相对于当前的命名空间,而 self::总是相对于包含的模块).

Side note: You can also write self:: at the beginning of a path to make it relative to the current module. This is more often used in use statements, since other paths are already relative (though they are relative to the current namespace, whereas self:: is always relative to the containing module).

这篇关于在单独的模块中使用时使用未声明的类型或模块 `std`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆