如何在 Rust 2015 中从一个模块到另一个模块进行基本的函数导入/包含? [英] How do I do a basic import/include of a function from one module to another in Rust 2015?

查看:21
本文介绍了如何在 Rust 2015 中从一个模块到另一个模块进行基本的函数导入/包含?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到如何将函数从一个文件(模块)包含(或导入、注入或其他词)到另一个文件(模块).

我开始一个新项目

$ cd ~/projects$货物新项目--bin$ cd 项目$树.|-- Cargo.toml-- 源代码|-- main.rs

我修改了 main.rs 并使用以下代码创建了一个新文件 a.rs(在 src 目录中):

ma​​in.rs

fn main() {println!("{}", a::foo());}

a.rs

pub fn foo() ->i32 { 42 }

我使用 cargo run 运行项目并得到错误:

错误[E0433]:无法解决:使用未声明的类型或模块`a`-->src/main.rs:2:20|2 |println!("{}", a::foo());|^ 使用未声明的类型或模块 `a`

看来我需要以某种方式导入a.我尝试将以下内容作为第一行添加到 main.rs

  • 使用一个;

    错误[E0432]:未解析的导入`a`-->src/main.rs:1:5|1 |用一个;|^ 根中没有 `a`

  • 使用a::*;

    错误[E0432]:未解析的导入`a`-->src/main.rs:1:5|1 |用一个::*;|^ 可能是丢失的`extern crate a;`?错误[E0433]:无法解决:使用未声明的类型或模块`a`-->src/main.rs:4:20|4 |println!("{}", a::foo());|^ 使用未声明的类型或模块 `a`

  • 使用a::foo;

    错误[E0432]:未解析的导入`a`-->src/main.rs:1:5|1 |使用 a::foo;|^ 可能是丢失的`extern crate a;`?错误[E0433]:无法解决:使用未声明的类型或模块`a`-->src/main.rs:4:20|4 |println!("{}", a::foo());|^ 使用未声明的类型或模块 `a`

  • extern crate a;使用 a::foo;

    错误[E0463]:找不到`a`的箱子-->src/main.rs:1:1|1 |外部板条箱一个;|^^^^^^^^^^^^^^^ 找不到箱子

  • extern crate proj;使用 proj::a::foo;

    错误[E0463]:找不到`proj`的箱子-->src/main.rs:1:1|1 |外部板条箱项目;|^^^^^^^^^^^^^^^^^^ 找不到箱子

我已经阅读了指南但是仍然不知道如何进行导入.

解决方案

在mainish模块(main.rs, lib.rs, or subdir/mod.rs)中,需要写mod a; 用于您要在整个项目(或子目录)中使用的所有其他模块.

在任何其他模块中,您需要编写 use a;use a::foo;

您远不是唯一对此感到困惑的人,当然可以做得更好,但是对模块系统的任何更改都将被拒绝为太令人困惑".

此答案是为Rust 2015"语言标准编写的.对Rust 2018"标准进行了更改,参见 这篇博文版本指南

I can't find how to include (or import, inject, or some other word) a function from one file (module) to another.

I start a new project with

$ cd ~/projects
$ cargo new proj --bin
$ cd proj
$ tree
.
|
-- Cargo.toml
-- src
   |
   -- main.rs

I modify main.rs and create a new file a.rs (inside the src dir) with the following code:

main.rs

fn main() {
    println!("{}", a::foo());
}

a.rs

pub fn foo() -> i32 { 42 }

I run the project with cargo run and get the error:

error[E0433]: failed to resolve: use of undeclared type or module `a`
 --> src/main.rs:2:20
  |
2 |     println!("{}", a::foo());
  |                    ^ use of undeclared type or module `a`

It seems that I need to import the a somehow. I tried to add following things as a first line to main.rs

  • use a;

    error[E0432]: unresolved import `a`
     --> src/main.rs:1:5
      |
    1 | use a;
      |     ^ no `a` in the root
    

  • use a::*;

    error[E0432]: unresolved import `a`
     --> src/main.rs:1:5
      |
    1 | use a::*;
      |     ^ maybe a missing `extern crate a;`?
    
    error[E0433]: failed to resolve: use of undeclared type or module `a`
     --> src/main.rs:4:20
      |
    4 |     println!("{}", a::foo());
      |                    ^ use of undeclared type or module `a`
    

  • use a::foo;

    error[E0432]: unresolved import `a`
     --> src/main.rs:1:5
      |
    1 | use a::foo;
      |     ^ maybe a missing `extern crate a;`?
    
    error[E0433]: failed to resolve: use of undeclared type or module `a`
     --> src/main.rs:4:20
      |
    4 |     println!("{}", a::foo());
      |                    ^ use of undeclared type or module `a`
    

  • extern crate a; use a::foo;

    error[E0463]: can't find crate for `a`
     --> src/main.rs:1:1
      |
    1 | extern crate a;
      | ^^^^^^^^^^^^^^^ can't find crate
    

  • extern crate proj; use proj::a::foo;

    error[E0463]: can't find crate for `proj`
     --> src/main.rs:1:1
      |
    1 | extern crate proj;
      | ^^^^^^^^^^^^^^^^^^ can't find crate
    

I have read the guide but still cannot figure out how to do imports.

解决方案

In a mainish module (main.rs, lib.rs, or subdir/mod.rs), you need to write mod a; for all other modules that you want to use in your whole project (or in the subdir).

In any other module, you need to write use a; or use a::foo;

You're far from the only person to be confused by this, and it's certainly possible to do better, but any changes to the module system will get rejected as "too confusing".

Edit: this answer was written for the "Rust 2015" language standard. Changes were made for the "Rust 2018" standard, see this blog post and the edition guide

这篇关于如何在 Rust 2015 中从一个模块到另一个模块进行基本的函数导入/包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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