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

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

问题描述

我找不到如何将功能从一个文件(模块)包含到另一个文件(模块)中.

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

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

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 }

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

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`

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

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.

    推荐答案

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

    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).

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

    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".

    此答案是针对"Rust 2015"语言标准编写的.对"Rust 2018"标准进行了更改,请参见

    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天全站免登陆